Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7956 Discussions

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

dnesteruk
Beginner
5,421 Views

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 }}).

0 Kudos
1 Reply
Judith_W_Intel
Employee
5,423 Views

 

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();
#else
 static bimap<string, Type> typeNames = createTypeNames();  // static data members must be const or constexpr
#endif
};

0 Kudos
Reply