3

Before asking this, I read previous question, but the issue is a bit different. I'm using this in my class:

static constexpr char* kSuffix = "tos"; 

Compiling with gcc with c++11 got me this error:

error: ISO C++ forbids converting a string constant to 'char*' [-Werror=write-strings] 

But constexpr is a stricter constraint than const does, so a constexpr is must a const, but not vice versa. So I wonder why is gcc not recognising constexpr in this case?

2

1 Answer 1

4

so a constexpr is must a const

Note that the constexpr is qualified on kSuffix itself, so the pointer becomes const (as char* const), but the pointee won't become const (as const char*). Gcc just wants to tell you that you should declare kSuffix as a pointer to const, i.e.

static constexpr const char* kSuffix = "tos"; 
Sign up to request clarification or add additional context in comments.

4 Comments

Is constexpr not qualified on kSuffix?
@fluter Yes. I revised the answer, does it become more clear?
I still wonder why constexpr take place of const here, they are in the same position essentially.
@fluter It's better to consider constexpr and const as different things; e.g. we can declare a const pointer like char* const and a pointer to const like const char*, but we can only write constexpr char* (or constexpr const char*) which means a constexpr pointer. (Also note we can't write sth like char* constexpr.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.