Search code examples
c++templatesconversion-operatorusing-declaration

Is `using Base::operator T` allowed where `T` is a template type parameter?


Consider this example:

struct B { operator int(); };

template<class T>
struct X:B
{
    using B::operator T;
};

GCC accepts the code, while Clang and MSVC rejects it. Which is correct?

Note that if the base type is dependent, all the compilers accept the code:

template<class T>
struct B { operator T(); };

template<class T>
struct X:B<T>
{
    using B<T>::operator T;
};

Solution

  • I think GCC is right, in §7.3.3/1, we can find:

    The set of declarations introduced by the using-declaration is found by performing qualified name lookup (3.4.3, 10.2) for the name in the using-declaration, excluding functions that are hidden as described below.

    I don't see any reason why operator T would not be found, actually:

    template<class T>
    struct X: B {
        T f () { return B::operator T; }
    };
    

    ...compiles fine with g++ and clang (did not test on MSVC).

    I cannot find anything in the standard specific to conversion functionsfor qualified name lookup except:

    Since specializations of member templates for conversion functions are not found by name lookup, they are not considered when a using-declaration specifies a conversion function (14.5.2).

    But B::operator int is not a specialization of a member function template, so it should not be taken into account by the above.