0

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

  1. Well-defined (my guess)
  2. Undefined behavior
  3. Or is it the wrong paragraph? If so, please point me to the correct location.
andreee
  • 4,459
  • 22
  • 42
  • @NathanOliver: Looking from that perspective, I agree. Just wondering now if I should vote for a dupe of my own post or simply delete the question (don't want to ask Meta SO for that...) – andreee Jun 07 '19 at 15:38
  • It would be a useful signpost IMHO. If you want I can dupe hammer it. – NathanOliver Jun 07 '19 at 15:39
  • 1
    Replacing comment that got auto removed: If you consider that in class initialization is just syntactic sugar for a member initialization list then the dupe answers if it is allowed. – NathanOliver Jun 07 '19 at 15:42
  • Now it's too late I guess :-) I would have answered my own question and explained the slight difference to the referenced one, but I think that most people will get the point when they read the other answer. – andreee Jun 07 '19 at 15:45

0 Answers0