Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constexpr and deprecated conversion warning

Tags:

c++

gcc

c++11

I'm writing a function as part of an experiment with Boost.Interprocess. In the function I assign a string literal to a variable declared constexpr char*. When I do this, I get:

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

My understanding of constexpr is that in a variable declaration it behaves as if the variable was declared const, but with the added stipulation that the variable must be initialized, and that initialization must be with a constant expression.

With this understanding I would expect constexpr char* to behave as const char*, and therefore not issue the warning. Am I missing something about how constexpr works?

I'm compiling with GCC 4.6.0 20110306 using -std=c++0x.

Any reasoning for the warning being issued would be appreciated. Thanks!

like image 680
mmoran Avatar asked Mar 13 '11 05:03

mmoran


2 Answers

The const from constexpr would make your variable char* const.

You still have the problem that the string literal is const char and that converting its address to char* is allowed, but deprecated.

like image 105
Bo Persson Avatar answered Oct 02 '22 06:10

Bo Persson


For another solution to this:

Instead of-

constexpr char* foo = "bar";

You can do-

constexpr char foo[] = "bar";

This will also get rid of the warning.

like image 20
sol_var Avatar answered Oct 02 '22 05:10

sol_var