Search code examples
c++templateslanguage-lawyertypedefambiguous

typedef and template parameter with same name


Why is that case incorrect (it's logical)

template <typename T>
struct Der: public Base
{
    typedef int T;
    T val;
};

, but that case is correct?

struct Base
{
    typedef int T;
};

template <typename T>
struct Der: public Base
{
    T val;
};

The Standard 14.6.1/7 says:

In the definition of a class template or in the definition of a member of such a template that appears outside of the template definition, for each base class which does not depend on a template-parameter (14.6.2), if the name of the base class or the name of a member of the base class is the same as the name of a template-parameter, the base class name or member name hides the template-parameter name (3.3.7).

Why is it not ambiguous in here?


Solution

  • The first example is incorrect according to [temp.local]/6:

    A template-parameter shall not be redeclared within its scope (including nested scopes).

    However, in

    template <typename T>
    struct Der: public Base
    {
        T val;
    };
    

    T is hidden by the name inherited from Base - as specified by your quote.

    [..] if the name of the base class or the name of a member of the base class is the same as the name of a template-parameter, the base class name or member name hides the template-parameter name (3.3.7).

    That is, the member val is of type int. Demo.