Search code examples
c++c++11initializationoperator-precedence

C++ evaluation order between brace-or-equal initializer and initialization-list?


Where I have this struct,

struct
AAA
{
    AAA() : bbb(2)
    {
        // ccc ???
    }

    int bbb = 1;
    int ccc = bbb;
};

AFAIK, if there's an initialization-list :bbb(2), the expression bbb = 1 will be ignored. And then, it's vague to me what ccc will become finally.

Which one of initialization-list or brace-or-equal initializer would be evaluated first? What's the rule between them?


Solution

  • The rule was always that fields are always initialised in order of declaration, and C++11 didn't change that. That means bbb's initialiser runs first, then ccc's initialiser runs. It doesn't matter whether either initialiser is specified on the field or as part of the constructor.