Pap*_*ter 11 c++ templates template-templates
如果我需要foo使用template-template参数定义模板函数,我通常会执行以下操作:
// Notice that the template parameter of class T is unnamed.
template <template <typename> class T> void f() { std::cout << "Yay!\n"; }
Run Code Online (Sandbox Code Playgroud)
请注意,template-template参数的template参数是未命名的,但我们可以为此参数指定一个名称:
// 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"; }
Run Code Online (Sandbox Code Playgroud)
这似乎没有用,因为我无法INNER在函数中提供参数,上面的代码会产生以下错误:
错误:'INNER'未命名类型
令我惊讶typename INNER的typename是,在为了命名类型之后,所有关键字都没有命名类型.无论如何,这很容易修复:
// 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!"
Run Code Online (Sandbox Code Playgroud)
但最后,INNER参数毕竟不需要名称:
// 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!"
Run Code Online (Sandbox Code Playgroud)
并且(确定您已经注意到了我之前)模板模板参数的参数中的名称被忽略了!它肯定被忽略了,因为如果不是它应该与第二个模板参数有名称冲突foo,不是吗?
忽略模板模板参数的参数名称的另一个演示:
// 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!"
Run Code Online (Sandbox Code Playgroud)
名称类型T是由template-template参数和模板模板本身同时使用的吗?我不这么认为,模板模板参数的名称被忽略了AFAIK.
那么,问题是什么?
至于有用的例子在#2,我指的是只能使用模板,模板参数指定的模板参数来实现的东西.
T.C*_*.C. 10
[basic.scope.temp]/P1:
模板的模板参数的名称的声明区模板参数是最小的模板参数列表 中导入了名.
(现在尝试说10次.)
它可以在该列表中使用.例如,
template < template<class T, T t> class TP > class foo {};
// ^ ^-----T's scope ends here
// |
// T can be used here
foo<std::integral_constant> bar;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
686 次 |
| 最近记录: |