1

In the following example below (c++11):

#include <iostream>

struct foo {
    foo() = default;
    foo(int x): v1{x} {}

    int v1 = 0;
    int v2 {v1};

    void print() const {
        std::cout<<"v1:"<<v1<<" v2:"<<v2<<std::endl;
    }
};

int main() {
    const auto bar1 = foo();
    bar1.print();  // prints v1:0 v2:0
    const auto bar2 = foo(42);
    bar2.print();  // prints v1:42 v2:42
}

I understand about the rule of v1. bar1.v1 is default initialized, and bar2.v1 is intialized by the constructor, with default initializer ignored.

What I am curious about is v2, even though bar2.v2 is default initialized, it will use the v1 that is initialized by the constructor. My question is: is this the correct behavior according to c++ standard? I cannot seem to find a good source for this.

2
  • 1
    Looks correct to me. Anything not specified in the member init list falls back to the in-class initializer. Commented Dec 29, 2022 at 8:26
  • Auzn: Did my answer answer your question or do you still have doubts? Commented Dec 30, 2022 at 16:49

1 Answer 1

2

Whether default initialized or initialized in the member initializer list, the member variables are initialized in the order of declaration, so yes, this is exactly how it should be.

class.base.init/13:

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

  • (13.1) First, and only for the constructor of the most derived class ([intro.object]), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
  • (13.2) Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
  • (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).
  • (13.4) Finally, the compound-statement of the constructor body is executed.
    [Note 6: The declaration order is mandated to ensure that base and member subobjects are destroyed in the reverse order of initialization. — end note]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.