在下面的代码中,我将类类型 R
和该类型的常量表达式作为模板参数传递。但 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'
<强> DEMO
在N4296::14.3.2 [temp.arg.nontype]
中,除了非类型模板参数应该是常量表达式之外,我找不到任何限制。
最佳答案
§14.1 [温度参数]/p4:
A non-type template-parameter shall have one of the following (optionally cv-qualified) types:
- 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
.
类类型不是这些。
关于c++ - 我们如何使用模板参数类型的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28379727/