Typedef template with all default arguments

I declare a template template with all parameters having default arguments, for example:

template<typename TYPE = int> class Foo {}; 

Then the following two are equivalent:

 Foo<int> one; Foo<> two; 

However, I am not allowed to just do:

 Foo three; 

Is it possible to achieve this with a typedef with the same name but without parentheses, for example:

 typedef Foo<> Foo; 
+6
source share
6 answers

If the declaration is typedef Foo<> Foo; allowed, after that the name Foo as a template cannot be specified. That is, the following becomes invalid.

 template< template< class > class > struct A {...}; A< Foo > a; // error 

Although the above typedef forbidden in practice, if you still need to write Foo as Foo<> , a macro like the following will do the trick.

 #define Foo Foo<> 
+1
source

I am doing something like the following, I don’t know if you like it or not:

 template<typename TYPE = int> class basic_Foo {}; typedef basic_Foo<int> Foo; 
+7
source

You cannot reuse a character with a different type, so anything you can do will not work as you expect. If you want to achieve this, use a different name as an alias:

 typedef Foo<> Foo_; 
+2
source
 typedef Foo<> Foo; 

It gives:

 prog.cpp:4: error: 'typedef class Foo<int> Foo' redeclared as different kind of symbol prog.cpp:2: error: previous declaration of 'template<class TYPE> class Foo' 

The error pretty much indicates what the problem is. The compiler sees Foo as redeclared.

However, this should compile and work:

 template<typename TYPE = int> class Foo {}; typedef Foo<> FooClone; int main() { Foo<int> one; Foo<> two; FooClone three; return 0; } 
+1
source

Not. Although you can declare a typedef for a class with the same name as class , because you can use typedef to override the name to indicate the type to which it already belongs.

 typedef class AA; 

or if A already declared as a class:

 typedef AA; 

You cannot do this with the template name (the template name is not a class name), you must give it a different name.

 typedef Foo<> Bar; 
+1
source

Unfortunately, no, because Foo already the name of the class template itself and therefore cannot be anything else in the same namespace.

0
source

Source: https://habr.com/ru/post/890289/