在代码中
template < template<class TTP> class TP > ... // whatever
TTP
那么在任何地方都可以使用吗?在标准中找不到关于这些名称发生了什么的任何参考。
在代码中
template < template<class TTP> class TP > ... // whatever
TTP
那么在任何地方都可以使用吗?在标准中找不到关于这些名称发生了什么的任何参考。
[basic.scope.temp]/p1:
模板模板参数的模板参数名称的声明区域是 其中引入名称的最小模板参数列表。
它可以在该列表中使用,仅此而已。例如,
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;
您可以访问它,您只需要稍微间接地了解它。
/--- don't bother giving this a name.
|
| Put it here instead ------------------\ |
| |
V V
template<template<typename, typename ...> class container_tmpl, typename value_t>
void foo(container_tmpl<value_t> x) {
std:: cout << __PRETTY_FUNCTION__ << std:: endl;
}
更准确地说,如果您有一个类型的对象vector<int>
并将其传递给foo
上面,则foo
可以访问相关的类型参数:
vector<int> v;
bar(v);
何时bar(v)
被调用,然后bar
“知道”第一个参数,这(我认为?)是您的目标。
我并不是说其他答案不正确,只是您问了一个稍微错误的问题。
要理解我给出的答案,忘记这template
条线可能更容易,而是看:
/* complex template-template gibberish */
void foo(container_tmpl<value_t> x) {
的类型x
,参数foo
,是类型container_tmpl<value_t>
。像or的东西在哪里,container_tmpl
像vector
or的东西在哪里。一旦你写了这个签名,很明显它是一个简单的类型(因此成为模板介绍),这是一个带有(至少)一个类型参数的模板。list
value_t
int
std::string
value_t
typename value_t
container_tmpl
在这种情况下,value_t
和container_tmpl
被定义在bar
.
如果你不明白为什么我有typename ...
,那么请记住它vector
实际上需要两种类型的参数,而不是一个。无论如何,基本思想是您必须在您期望获得它们的位置之外为这些模板参数提供名称。例如,如果您有一个带有三个参数、两个类型参数和一个整数的模板。
template< template<typename,int,typename> class the_template, typename T1, int I, typename T2>
void foo(the_template<T1,I,T2> x);
No. It's a template parameter for TP<>, not the outer template function.