【发布时间】:2015-07-19 18:22:53
【问题描述】:
假设我们有
template <const char*>
struct A{};
// static storage
const char a[] = "asd";
const char* p = "asd";
这个实例化
A<a>{};
对编译器来说没问题。这是可以理解的——数组a 衰减为指向第一个元素的指针。但是如果我们像这样用p 实例化A
A<p>{};
编译器报错:
错误:'char *' 类型的非类型模板参数不是常量表达式
为什么标准不允许指定类型为const char* 的命名变量或仅指定字符串文字"asd",即btw lvalue 本身,作为模板参数?
【问题讨论】:
-
我不相信它与左值有关,因为
p没有精确的内存位置,但a有。 -
这和
template <int> struct X{}; int p = 5; X<p> x;没有区别