[英]How can we use a variable of the type of the template parameter?
In the code below, I passed both class type R
and a constant expression of that type as template parameters. 在下面的代码中,我同时传递了类类型R
和该类型的常量表达式作为模板参数。 But clang doesn't accept that: 但是clang不接受:
#include <iostream>
template<class T, T t>
void foo(){ std::cout << "foo()" << std::endl; }
class R
{
public:
int f;
constexpr R(): f(15){ }
};
constexpr R r;
int main(){ foo<R, r>(); } //note: candidate template ignored:
//invalid explicitly-specified argument
//for template parameter 't'
In N4296::14.3.2 [temp.arg.nontype]
I can't find any restriction other than the non-type template parameter should be a constant expression. 在N4296::14.3.2 [temp.arg.nontype]
除了非类型模板参数应为常量表达式外,我找不到任何限制。
§14.1 [temp.param]/p4: §14.1[temp.param] / p4:
A non-type template-parameter shall have one of the following (optionally cv-qualified) types: 非类型模板参数应具有以下(可选,具有cv限定)类型之一:
- integral or enumeration type, 整数或枚举类型,
- pointer to object or pointer to function, 指向对象或函数的指针,
- lvalue reference to object or lvalue reference to function, 对对象的左值引用或对函数的左值引用,
- pointer to member, 指向成员的指针,
std::nullptr_t
.std::nullptr_t
。
A class type is none of these. 类类型不是这些。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.