The following code unsurprisingly fails when used:
struct Foo {
std::string b = a + " world"; // a not declared yet
std::string a = "Hello";
};
because we're attempting to use a
before having defined it and due to [class.base.init]/13.3:
Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
But what about this code?
struct Foo {
std::string a = "Hello";
std::string b = a + " world";
};
13.3 seems reasonable here as well (making the code well-defined), however I am not sure that it covers the situation in the listing above, because it mentions mem-initializers but misses this specific case (IMO). I admit this may simply be a lack of English skills, so I prefer to ask.
Does the referenced paragraph in the standard make the code
- Well-defined (my guess)
- Undefined behavior
- Or is it the wrong paragraph? If so, please point me to the correct location.