A member with in-class initializer must be const. What?

A member with in-class initializer must be const. What?

I have explicitly enabled C++0x support in my project (latest C++ composer version) and I'm getting the above error. What is going on? The code I'm using is

static bimap<string, Type> typeNames = createTypeNames();

It doesn't seem like this works, and neither does inline initialization (i.e., typeNames = { { "Foo", Type::abc }}).

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.


Is typeNames being declared inside a class (i.e. a static data member)?

If so it is telling you that it needs to be declared const or constexpr (in c++11 mode). Since you are initializing it with a function call it must be constexpr and that function must itself be constexpr.

Example below. Compile with -DOK -std=c++11 it compiles fine but without -DOK it will give you the above error:

struct string {};
struct Type {};

template <class T, class T2>
struct bimap {
  constexpr bimap() {}

constexpr bimap<string,Type> createTypeNames() {
   return bimap<string, Type>();

struct C {
#ifdef OK
 static constexpr bimap<string, Type> typeNames = createTypeNames();
 static bimap<string, Type> typeNames = createTypeNames();  // static data members must be const or constexpr

Leave a Comment

Please sign in to add a comment. Not a member? Join today