constexpr和不推荐的转换警告

mmo*_*ran 11 c++ gcc c++11

我正在编写一个函数作为Boost.Interprocess实验的一部分.在函数中,我将字符串文字分配给声明的变量constexpr char*.当我这样做时,我得到:

warning: deprecated conversion from string constant to char* [-Wwrite-strings].

我的理解constexpr是,在变量声明中,它的行为就像声明了变量一样const,但是添加了规则,必须初始化变量,并且初始化必须使用常量表达式.

有了这种理解,我希望constexpr char*表现为const char*,因此不发出警告.我错过了一些有关constexpr工作的方法吗?

我正在使用-std = c ++ 0x编译GCC 4.6.0 20110306.

发出警告的任何理由都将受到赞赏.谢谢!

Bo *_*son 11

constconstexpr将使您的变量char* const.

您仍然遇到字符串文字的问题,const char并且char*允许将其地址转换为,但已弃用.


sol*_*var 9

对于另一个解决方案:

代替-

constexpr char* foo = "bar";
Run Code Online (Sandbox Code Playgroud)

你可以做-

constexpr char foo[] = "bar";
Run Code Online (Sandbox Code Playgroud)

这也将摆脱警告.