fluter
fluter

Reputation: 13836

C++ String literal and constants

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?

Upvotes: 3

Views: 267

Answers (1)

songyuanyao
songyuanyao

Reputation: 173014

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";

Upvotes: 4

Related Questions