具有默认类型的可变参数模板

Variadic template with deafult type

我正在尝试使用默认指针类型创建参数包:

template<int*... T>
void f() {}

我收到此代码的错误:

int main(){
    int* a = new int(5);
    f<a>();
}

但是如果我这样做,我不会收到错误消息:

int main(){
    f<nullptr>();
}

为什么?

错误:

./example.cpp: In function 'int main()': ./example.cpp:6:7: error: the
value of 'a' is not usable in a constant expression
     f<a>();
       ^ ./example.cpp:5:10: note: 'a' was not declared 'constexpr'
     int* a = new int;
          ^ ./example.cpp:6:10: error: no matching function for call to 'f<a>()'
     f<a>();
          ^ ./example.cpp:2:6: note: candidate: 'template<int* ...T> void f()'  void f() {}
      ^ ./example.cpp:2:6: note:   template argument deduction/substitution failed: ./example.cpp:6:10: error: 'a' is not a
valid template argument because 'a' is a variable, not the address of
a variable
     f<a>();
          ^ Compiler returned: 1

Compiler Explorer

non-type 模板参数的参数必须是 constant expression

int* a = new int(5);

在此代码段中,a 不是常量表达式,不适合模板 non-type 参数。

其实这在直觉上应该是可以接受的。请记住,编译器需要在程序执行之前为实例化模板生成代码,并且该代码必须使用模板参数的值。但是 a 的值在编译时是未知的——它可以是任何东西,这取决于内存的分配位置。显然,编译器无法预测它的值。

另一方面,全局地址(或静态函数)object 可用作 non-type 指针模板参数:

int k = 10;

int main(){
    f<&k>();
}

上面的代码有效,这在直觉上是有道理的,因为编译器会知道全局 object 所在的位置(编译器会自己把它放在那里!)

最后但并非最不重要的一点是,您的标题不必要地狭窄。这些规则以相同的方式适用于 non-variadic 个模板。