I declare a templated cl开发者_运维知识库ass 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'm not allowed to just do:
Foo three;
Is it possible to achieve that with a typedef to the same name but without the brackets, like this:
typedef Foo<> Foo;
I do something like the following, I don't know if you will like it or not:
template<typename TYPE = int>
class basic_Foo {};
typedef basic_Foo<int> Foo;
You can't redeclare a symbol with a different type, so whatever you will be able to do won't work as you expect. If you want to achieve this, use a different name as alias :
typedef Foo<> Foo_;
If the declaration typedef Foo<> Foo; is allowed, thereafter 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
Though the above typedef isn't allowed in practice,
if you still need to write Foo as Foo<>, a macro like the following
will meet the purpose.
#define Foo Foo<>
typedef Foo<> Foo;
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 tells what the problem is. Compiler sees Foo as being re-declared.
However, this shall compile and work:
template<typename TYPE = int> class Foo {};
typedef Foo<> FooClone;
int main()
{
Foo<int> one;
Foo<> two;
FooClone three;
return 0;
}
No. Although you can declare a typedef for a class with the same name as a class because you can use a typedef to redefine a name to refer to the type to which it already refers.
typedef class A A;
or if A was already declared as a class:
typedef A A;
You can't do that with the name of a template (the name of a template isn't a name of a class), you'd have to give it a different name.
typedef Foo<> Bar;
Unfortunately, no, because Foo is already the name for the class template itself, and thus can't be anything else in the same namespace.
加载中,请稍侯......
精彩评论