David Pham David Pham - 2 months ago 5
C++ Question

Initialize a static const list of strings

I need to initialize a

static const std::list<std::string>
in my .h. But, how do I do ?

class myClass {
static const std::list<std::string> myList = {"a", "b", "c"};
}


Thanks.

Answer

How do I initialize a static const std::list in my .h?

No you can't directly do that.

To initialize a const static data member inside the class definition, it has to be of integral (or enumeration) type; that as well if such object only appears in the places of an integral-constant expression. For more details, plese refer C++11 standard in the following places.

$9.4.2 Static data members and
$3.2 One Definition rule

But, you MAY be able to do something like this: How can you define const static std::string in header file?