Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedef Foo<> Foo compiles but is it valid?

The following bit of code compiles in VS2008 and GCC 4.8.2

template<typename T=void>
struct Foo
{
};

// typedef Foo<> Foo;   // Does *NOT* compile

int main()
{
    typedef Foo<> Foo;
    Foo f1;

   // Foo<char> f2;     // Does *NOT* compile
   //::Foo<char> f3;    // COMPILES
}

Is it valid?

like image 787
Olumide Avatar asked Oct 17 '14 18:10

Olumide


1 Answers

As per C++11 3.3.10/1:

A name can be hidden by an explicit declaration of that same name in a nested declarative region or derived class.

(Emphasis mine)

That's why the template name Foo can be hidden by the typedef name Foo inside main() (a different scope), but not in the same scope as the template name is declared.

As to why this similar case is legal:

struct Foo
{
};

typedef Foo Foo;   // *DOES* compile

That is explicitly allowed by 7.1.3/3:

In a given non-class scope, a typedef specifier can be used to redefine the name of any type declared in that scope to refer to the type to which it already refers.

like image 200
Angew is no longer proud of SO Avatar answered Oct 06 '22 09:10

Angew is no longer proud of SO