简体   繁体   English

我们如何使用模板参数类型的变量?

[英]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'

DEMO DEMO

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.

相关问题 可以使用MemberFunction类型作为模板参数吗? - can we use MemberFunction type as template parameter? 我们如何在c ++中检查模板参数的类型? - How can we check the type of the template parameter in c++? 我可以在模板中为非类型参数使用类型参数吗? - Can I use a type parameter in a template for a non-type parameter? 如何使用模板 class 作为同一模板 class 的类型参数 - How to use a template class as a type parameter for the same template class 如何在C ++中使用带有模板的类型作为模板参数 - How to use a type with template as a template parameter in C++ 如何在 class 模板中使用定义并与模板参数关联的类型? - How to use a type defined and associated with a Template parameter in a class Template? 如何将模板类中的模板类用作带模板参数的模板模板参数? - How can I use template class inside of template class as a template template parameter with a template parameter? 如何使用std :: conditional根据模板参数类型设置类型 - How to use std::conditional to set type depending on template parameter type 如何将浮点值用作非类型模板参数? - How can I use a floating-point value as a non-type template parameter? 如何使用decltype作为模板化类的返回类型的模板参数? - How can I use decltype as the template parameter for a return type of a templated class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM