Why must initializer list order match member declaration order?

The warning is indicating that regardless of the order you use in the constructor initialization list the standard requires that non-static data members be initialized in the order they were declared. We can see this by going to the draft C++ standard section 12.6.2 Initializing bases and members paragraph 10 which says:

In a non-delegating constructor, initialization proceeds in the
following order:

and includes:

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

Why does the standard require this? We can find a rationale for this in paper The Evolution of C++: 1985 to 1989 by Bjarne Stroustrup in section 6 it says:

The initialization takes place in the order of declaration in the
class with base classes initialized before members,

[…]

The reason for ignoring the order of initializers is to preserve the
usual FIFO ordering of constructor and destructor calls. Allowing two
constructors to use different orders of initialization of bases and
members would constrain implementations to use more dynamic and more
expensive strategies

Leave a Comment