0
  1. Having a class, the initialization order of its members is strongly defined in constructor (maybe to allow dependencies between members, like in this question - but I think this is more a design problem, I can imagine circular dependencies).
  2. Having a function call, the order of parameters evaluation isn't defined, I think it's for C compatibility.
  3. And we have an error for the following "dreaded diamond" problem:

    struct A {virtual void Print() {}};
    struct B: virtual public A {virtual void Print() {}};
    struct C: virtual public A {virtual void Print() {}};
    struct D: public B, public C {};
    

    The compiler doesn't know which version to choose, the order being defined as ambiguous. Why simply not use a "left-to-right depth first resolution order" as defined in this question (with an unconvincing answer), namely to choose B over C?

So why these different approaches? Why there's a strict order for 1 and not for 3? Wasn't more simple to keep 1 undefined? Or 2 simply defined as left-to-rigth?

Community
  • 1
  • 1
Liviu
  • 1,859
  • 2
  • 22
  • 48
  • The issue with trying to access a member with the same name from different bases is well defined as being ambiguous. This is quite different from the order of evaluation of function arguments being implementation-defined. – Vaughn Cato Jan 04 '14 at 02:55
  • 1
    There are many things in C++ that are defined as being ambiguous to avoid mistakes. – Vaughn Cato Jan 04 '14 at 02:58
  • Well, unspecified argument evaluation order opens up some optimization opportunities. – ScarletAmaranth Jan 04 '14 at 03:00
  • @Vaughn Cato "well defined as being ambiguous" is not a "defined order". Why the initialization order isn't (well) defined as ambiguous (to avoid mistakes ... especially in the tests)? – Liviu Jan 04 '14 at 03:03
  • If the initialization order of members was ambiguous, then it would be impossible to initialize anything. It is possible that it could be unspecified, but that would make certain things much more difficult. – Vaughn Cato Jan 04 '14 at 03:11
  • Take a look at this answer to get a better idea of the differences: http://stackoverflow.com/q/2397984/951890 – Vaughn Cato Jan 04 '14 at 03:13
  • @Vaughn Cato Sorry for my mistake, I know the nuances, maybe the question should be edited, but I won't stay chatting with you anymore – Liviu Jan 04 '14 at 03:17
  • @Vaughn Cato I modified the question, is it more clear now? – Liviu Jan 04 '14 at 13:03
  • Order 1, the initialization order of class members: "The reason for this language design decision is to ensure there is a unique order to destroy members; otherwise, the destructor would have to destroy objects in different orders, depending on the constructor that built the object. The bookkeeping overhead for that was deemed unacceptable." ("C++ Coding Standards: 101 Rules, Guidelines, and Best Practices " by Herb Sutter, Andrei Alexandrescu) This is to allow dependencies between class members, I think. – Liviu Jun 02 '14 at 12:21

1 Answers1

1

These are very different situations with various trade-offs. In each case you have to consider

  • How often do these situations occur?
  • How likely is an arbitrary choice to have unexpected consequences?
  • How easy is it to explicitly specify when a specific order is needed?
  • What performance penalties are caused by forcing a particular order?

The answers to these questions are quite different in each situation, so having different choices is natural.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • I'm sorry, but this is philosophy. I believe that the people (Stroustrup alone in the 80s ?) deciding these matters had more simpler arguments for doing what they did and they didn't have the meanings for questions like performance, frequency of occurence, etc. – Liviu Jan 06 '14 at 09:55
  • It doesn't matter much, but to me the most "natural" in all the three cases is to have a left-to-right order ... except for the arabs and the jews. (The order of initialization in constructor being left-to-right) – Liviu Jan 06 '14 at 09:58
  • @Liviu: Well, you're wrong. And this is an excellent, accurate answer. – Lightness Races in Orbit Sep 12 '15 at 16:21