So, the C++ standard requires that class members be initialized in the order in which they are declared in the class, rather than the order that they're mentioned in any constructor's initializer list. However, this doesn't imply anything about the order in which the arguments to those initializations are evaluated. I'm working with a system that frequently passes references to serialization objects around, and wondering if I can ensure that bits are read from it in the right order, independent of the order in which those bits get written into the object's fields.
struct Foo {
int a;
double b;
// I want to be able to do this
Foo(SerObj &s)
: b(s.readDouble()), a(s.readInt())
{ }
// Rather than this
Foo (SerObj &s)
{
b = s.readDouble();
a = s.readInt();
}
};
Obviously, reordering things like ints
and doubles
in the declaration is not too big a deal, but bigger objects and things requiring dynamic allocation sometimes can be.
C++ Standard
12.6.2/3
:The order of the initialization is the one you specified in the question. Evaluation is part of this initialization, and the initializations can't interleave (because there is a sequence point between them).
That means that the function calls in your initializer lists are not called in the desired order, but in the order in which the member declarations appear.