首页 > 解决方案 > 模板类友元函数的内部类

问题描述

标签: c++templatesinner-classes

解决方案


It's a non-deduced context.

Ostensibly you could have a definition like

template<typename AB>
bool operator == (const AB& b1, const AB& b2)
{
    return b1.b == b2.b;
}

but it's too broad as it catches all types. Yoiy can restrict it this way

template<typename AB>
auto operator == (const AB& b1, const AB& b2) ->
     std::enable_if_t<std::is_same_v<AB, typename A<decltype(b1.b)>::B>, bool>
{
    return b1.b == b2.b;
}

推荐阅读