Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedef a template with all default arguments

I declare a templated class 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;
like image 228
Alan Turing Avatar asked Jun 11 '11 15:06

Alan Turing


People also ask

CAN default arguments be used with the template?

You cannot give default arguments to the same template parameters in different declarations in the same scope. The compiler will not allow the following example: template<class T = char> class X; template<class T = char> class X { };

Can template parameters have default values?

Just like in case of the function arguments, template parameters can have their default values. All template parameters with a default value have to be declared at the end of the template parameter list.

How do you use template arguments in C++?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

What is Typedef Typename?

typedef is defining a new type for use in your code, like a shorthand. typedef typename _MyBase::value_type value_type; value_type v; //use v. typename here is letting the compiler know that value_type is a type and not a static member of _MyBase . the :: is the scope of the type.


1 Answers

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;
like image 177
Cem Kalyoncu Avatar answered Sep 19 '22 09:09

Cem Kalyoncu