Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous template typename/class declarations

Tags:

c++

templates

I'm curious as to why anonymous template typename/class declarations are allowed, such as the following:

template <typename, class, typename> struct TemplateTest1 { int a; float b ; } ;
TemplateTest1 <int, int, int> tt1 ;

Can anyone explain the practical value of these anonymous types? Do they affect the expression of the templated structure?

like image 972
Gearoid Murphy Avatar asked Jul 16 '11 14:07

Gearoid Murphy


2 Answers

By anonymous, I assume you meant unnamed template parameter.

It's allowed, because sometimes you may not need the template argument, and so making it anonymous makes it clear to programmer that the argument is not used anywhere in the class, although it's not that necessary.

It is similar to the way a function with an unnamed parameter is allowed:

void f(int) //allowed
{
}
like image 94
Nawaz Avatar answered Oct 07 '22 10:10

Nawaz


The programmer may choose to typedef a particular template-instantiation, which should be used only with that type. One type may have <int,int,bool>, another type maybe <float, bool string>, and the programmer doesn't want them to be convertible. Underlying structure is same, but they aren't convertible.

It is like:

struct ABC
{ int a,b;};
struct XYZ
{ int a,b;};

Both types are same, but ABC is not convertible to XYZ and vice versa. Many of Windows handles are declared via DECLARE_HANDLE, and are not convertible.

like image 37
Ajay Avatar answered Oct 07 '22 10:10

Ajay