【问题标题】:How can we use a variable of the type of the template parameter?我们如何使用模板参数类型的变量?
【发布时间】:2015-02-07 15:43:22
【问题描述】:

在下面的代码中,我将类类型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]我找不到除了非类型模板参数应该是常量表达式之外的任何限制。

【问题讨论】:

标签: c++ templates


【解决方案1】:

§14.1 [temp.param]/p4:

非类型模板参数应具有以下之一 (可选 cv 限定)类型:

  • 整数或枚举类型,
  • 指向对象的指针或指向函数的指针,
  • 对对象的左值引用或对函数的左值引用,
  • 指向成员的指针,
  • std::nullptr_t

类类型不是这些。

【讨论】:

猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多