I have a following nested template class inside another template class:
template<typename T>
struct A
{
template<typename V>
struct B {};
};
What would be the signature of a non-member operator==
for the nested type B
? The following naïve attempt does not work:
template<typename T, typename V>
bool operator==(A<T>::B<V> left, A<T>::B<V> right);
Clang, GCC and MSVC gives various different errors and/or hints what is wrong such as missing template
keyword but none of my attempts to resolve it worked out.
Note that this obviously works:
template<typename T>
struct A
{
template<typename V>
struct B {};
template<typename V>
friend bool operator==(B<V> left, B<V> right)
{
return true;
}
};
However the reason I need the out of line non-member declaration is to document it using qdoc. The qdoc is using clang to parse the sources and it requires me to provide the declaration of the operator==
that I have actually implemented in place like just shown.
A direct approach,
typename A<T>::template B<V> left
, does not work as intended becauseA<T>::
is a nested-name-specifier, which is a non-deduced context.T
has to be passed explicitly:operator==<int>(a, b)
.To keep
==
usable as a binary operator, an out-of-line non-member declaration can be implemented using SFINAE:Full example on godbolt: click.