Is there any use for named parameters in template template parameters

If I need to define a template foo with a template-template parameter, I usually do the following:

 // Notice that the template parameter of class T is unnamed. template <template <typename> class T> void f() { std::cout << "Yay!\n"; } 

Please note that the template parameter of the template-template parameter is not specified, but we can assign a name to this parameter:

 // Now the template parameter of class T is named INNER. template <template <typename INNER> class T> void f(const INNER &inner) { std::cout << inner << " Yay!\n"; } 

This does not seem useful at all, because I cannot cancel the INNER parameter in a function, the above code causes the following error:

error: "INNER" does not name type

It typename INNER me that typename INNER does not name a type, because the keyword typename exists to indicate a type. Anyway, this is easy to fix:

 // Now INNER is the name of the template parameter of class T and also // the name of the second template parameter of foo. template <template <typename INNER> class T, typename INNER> void f(const INNER &inner) { std::cout << inner << " Yay!\n"; } // ... f<std::valarray, int>(666); // Prints "666 Yay!" 

But in the end, the INNER parameter INNER not need a name after all:

 // Now the template parameter of class T is unnamed one more time, // INNER is the name of the second template parameter of foo. template <template <typename> class T, typename INNER> void f(const INNER &inner) { std::cout << inner << " Yay!\n"; } // ... f<std::valarray, int>(666); // Prints "666 Yay!" 

And (of course, you already noticed before me) the name in the template-template parameter is ignored! It was ignored, because if it should not have a name clash with the second parameter of the foo template, right?

Another demonstration of ignoring the template parameter name:

 // Now T is the name of the template parameter of class T and also // the name of the template parameter of foo! template <template <typename T> class T> void f() { std::cout << "Yay!\n"; } // ... f<std::valarray>(); // prints "Yay!" 

Is the type named T used by the template-template parameter and the template-template itself at the same time? I donโ€™t think that the name in the template is ignored by AFAIK.

So what is the question?

  • Am I thinking right? Are parameter names of a named template template parameter template ignored?
  • If I am mistaken, and I misunderstood all this, is there a use for the named parameters in the template-template parameters? Can you give some useful examples?

As for the useful examples on # 2, I mean something that could be achieved using the parameters of the named template of the template-template parameters.

+7
c ++ templates template-templates
source share
1 answer

[basic.scope.temp] / p1:

The declarative area of โ€‹โ€‹the template parameter name template template-parameter is the smallest list of template parameters in which the name was entered.

(Now try saying that 10 times.)

It can be used inside this list. For example,

 template < template<class T, T t> class TP > class foo {}; // ^ ^-----T scope ends here // | // T can be used here foo<std::integral_constant> bar; 
+8
source share

All Articles