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 const开发者_JAVA百科expr
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!
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.
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.
精彩评论