C++ 字符串文本和常量
C++ String literal and constants
在问这个问题之前,我读了上一个问题,但问题有点不同。 我在课堂上使用它:
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
?
所以
constexpr
必须是一个const
请注意,constexpr
是对kSuffix
本身限定的,因此指针变为const
(如char* const
(,但指针不会变为const
(如const char*
(。gcc 只是想告诉你,你应该声明kSuffix
为指向const
的指针,即
static constexpr const char* kSuffix = "tos";
相关文章:
- C++ 在类中使用常量引用文本时 O2 内存泄漏
- C++ 字符串文本和常量
- 调用 cout 时如何在结构中输出常量文本?
- 将字符串文本常量定义为 char const* 和 wchar const*
- 检测编译时文本和常量
- 是否有必要在模板化函数中强制转换文本常量?
- C++编译器如何在引用参数中实际传递文本常量?
- 在模板实例化期间,文本值不被视为常量表达式
- 将文本字符串作为常量字符 * 参数传递会导致代码分析器错误
- 在 c++ 中存储文本常量
- 常量表达式中的非文本类型'compare'
- 声明具有常量引用与常量变量的常量文本字符串
- G++ 将返回的字符串文本视为常量字符指针而不是常量字符数组
- 将文本文件转换为常量字符指针
- 在编译时常量中对字符串文本进行下标
- C11/C++11 宏,用于将常量字符串与文本整数相关联
- 为什么将整数文本类型转换为指针值会导致非常量表达式
- 在文本/常量模板参数中使用变量 contet
- 从文本文件中读取行数,并将其存储为数组大小为c++的常量int
- C++从文本文件读取常量字符数组