Const constexpr char * vs. constexpr char *

I know the difference between const and constexpr. One of them is a compilation time constant, and the other is compilation time or a run-time constant.

However, for an array of characters / strings, I am confused why the compiler complains that one of them is used over the other.

For example, I have:

constexpr char* A[2] = {"....", "....."}; const constexpr char* B[2] = {"....", "....."}; 

With the declaration "A" I get:

 ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] 

but with the declaration "B" I do not receive any warnings.

Why does the optional constant determinant get rid of the warning? Aren't they both "const char *"? I ask because both are declared using constexpr , which should make it const char* by default?

I would expect A to be fine: S

+6
source share
1 answer

const tells the compiler that the characters you point to should not be written.

constexpr tells the compiler that the pointers that you have stored in these arrays, you can fully appreciate at the time of compilation. However, he does not say whether characters indicating pointers can appear.

By the way, in another way, you could write this code:

 const char * const B[2]; 

The first const is applied to characters, and the second const is applied to the array itself and the pointers containing it.

+8
source

Source: https://habr.com/ru/post/988271/


All Articles