Search code examples
c++templateslanguage-lawyername-lookup

Difference in behaviour between clang and gcc when trying to confuse them by using a template alias with a dummy parameter to conceal the base class


Consider the following c++ program:

class A
{
    protected:
        int x;
};

template<typename X>
using B = A;

template<typename T>
class C : public B<T>
{
    public:
        void f()
        {
            x = 0;
        }
};

int main()
{
}

When compiled with clang and gcc using -std=c++17 -pedantic-errors as compilation options they behave differently: Clang compiles without any errors, but gcc gives a compilation error about not being able to lookup the identifier x.

What does the c++ standard say in this case? Are both behaviours allowed, or does one of the compilers have a bug in this case?

Compiler explorer link: https://godbolt.org/z/EYvYrr


Solution

  • This looks similar to CWG1390, and looks like Clang's behavior is consistent with CWG agreement on how such alias templates should be handled (substituted eagerly, at template definition time):

    1390. Dependency of alias template specializations

    According to 17.7.2.1 [temp.dep.type] paragraph 8, a type is dependent (among other things) if it is

    • a simple-template-id in which either the template name is a template parameter or any of the template arguments is a dependent type or an expression that is type-dependent or value-dependent

    This applies to alias template specializations, even if the resulting type does not depend on the template argument:

       struct B { typedef int type; };
       template<typename> using foo = B;
       template<typename T> void f() {
         foo<T>::type * x;  //error: typename required
       }
    

    Is a change to the rules for cases like this warranted?

    Notes from the October, 2012 meeting:

    CWG agreed that no typename should be required in this case. In some ways, an alias template specialization is like the current instantiation and can be known at template definition time.