c++ - constexpr const vs constexpr variables? -


this question has answer here:

it seems obvious constexpr implies const , common see:

constexpr int foo = 42; // no const here 

however if write:

constexpr char *const str = "foo"; 

then gcc spawn "warning: deprecated conversion string constant ‘char*’" if -wwrite-string flag passed.

writing:

constexpr const char *const str = "foo"; 

solves issue.

so constexpr const , constexpr same?

the issue in variable declaration, constexpr applies const-ness object declared; const on other hand can apply different type, depending on placement.

thus

constexpr const int = 3; constexpr int = 3; 

are equivalent;

constexpr char* p = nullptr; constexpr char* const p = nullptr; 

are equivalent; both make p const pointer char.

constexpr const char* p = nullptr; constexpr const char* const p = nullptr; 

are equivalent. constexpr makes p const pointer. const in const char * makes p point const char.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -