Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
The Intel sign-in experience has changed to support enhanced security controls. If you sign in, click here for more information.
7782 Discussions

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

0 Kudos
1 Reply


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