相关疑难解决方法(0)

指针非类型模板参数

我真的不明白为什么下面的代码不能编译:

template<const char*>
struct Foo{};

constexpr const char s1[] = "test1";
constexpr const char* const s2 = "test2";

int main()
{
    Foo<s1> foo1; // ok
    // Foo<s2> foo2; // doesn't compile
}
Run Code Online (Sandbox Code Playgroud)

取消注释最后一main()行使g ++和clang ++发出错误

error: 's2' is not a valid template argument because 's2' is a
variable, not the address of a variable
Run Code Online (Sandbox Code Playgroud)

error: non-type template argument for template parameter of
      pointer type 'const char *' must have its address taken
Run Code Online (Sandbox Code Playgroud)

分别.

我的问题是:

  1. 为什么s1实例化 …

c++ templates c++11

17
推荐指数
3
解决办法
1594
查看次数

标签 统计

c++ ×1

c++11 ×1

templates ×1