Why const char* static field has to be constexpr to initialize it inside class?

49 views Asked by At

I have the following piece of code:

struct st
{
  static constexpr const int x = 2;
  static constexpr int x2 = 2;
  static const int x3 = 2;
  static const char* str = "BLAH"; // ERROR
};

marked line gives me the following error:

error: ‘constexpr’ needed for in-class initialization of static data member ‘const char* st::str’ of non-integral type [-fpermissive]

When I add constexpr to it, it's all dandy.

struct st
{
  // ...
  static constexpr const char* str = "BLAH"; // OK
};

Why is it like this? Since initialization of non plain types (e.g. std::string or user defined types) are not allowed in class body why const char* is but only with constexpr?

0

There are 0 answers