C ++字符串文字和常量

问题描述 投票:1回答:1

在问这个之前,我读了previous question,但是问题有点不同。我在课堂上使用它:

static constexpr char* kSuffix = "tos";

[使用c ++ 11用gcc编译时出现此错误:

error: ISO C++ forbids converting a string constant to 'char*' [-Werror=write-strings]

但是constexpr是比const严格的约束,因此constexpr必须是const,但反之则不然。所以我想知道为什么在这种情况下gcc无法识别constexpr

c++ string c++11 const constexpr
1个回答
0
投票

所以constexpr必须是const

请注意,constexpr限定于kSuffix本身,因此指针变为const(作为char* const),但指针不会变成const(作为const char*)。 Gcc只是想告诉您,您应该将kSuffix声明为指向const的指针,即

static constexpr const char* kSuffix = "tos";
© www.soinside.com 2019 - 2024. All rights reserved.