Anonymous typename / class name templates

I'm curious why declarations of the anonymous typename / class template are allowed, for example:

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 template structure?

+7
source share
2 answers

By anonymous, I assume that you were referring to a parameter without a template name.

This is allowed because sometimes you may not need a template argument, and therefore makes it anonymous, it makes it clear to the programmer that the argument is not used anywhere in the class, although this is not necessary.

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

 void f(int) //allowed { } 
+9
source

The programmer can choose a specific template instance for typedef that should only be used with this type. One type can have <int,int,bool> , another type can be <float, bool string> , and the programmer does not want them to be convertible. The basic structure is the same, but they are not convertible.

What is it like:

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

Both types are the same, but ABC does not convert to XYZ and vice versa. Many Windows descriptors are declared through DECLARE_HANDLE and are not converted.

+3
source

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


All Articles