c++constructorlanguage-lawyerinitializer-listctor-initializer

Is member initializer list considered part of the body of a constructor or it it considered part of the declarator


I am learning about member initializer lists in C++. So consider the following example:

struct Person
{
    public:
        Person(int pAge): age(pAge)
//                        ^^^^^^^^^ is this member initializer formally part of the constructor body? 
        {
        }
    private:
        int age = 0;
};

My first question is that is the member initializer age(pAge) formally part of the constructor's body. I mean i've read that a function's body starts from the opening { and end at the closing }. To my current understanding, there are four things involved here:

  1. Ctor definition: This includes the whole
//this whole thing is ctor definition
Person(int pAge): age(pAge)
        {
        }
  1. Member initializer: This is the age(pAge) part.

  2. Ctor declaration: This is the Person(int pAge) part.

  3. Ctor's body: This is the region between the opening { and the closing }.

My second question is that is the above given description correct? If not then what should be the correct meaning of those four terms according to the C++ standard: Ctor definition, Member initializer, Ctor declaration and Ctor's body.

PS: I've read this post which doesn't answer my question.


Solution

  • As per [dcl.fct.def.general], which tells us the grammar of a function definition, a ctor-initializer is part of the function-body:

    function-definition:
        [...] function-body
    
    function-body:
        ctor-initializer_opt compound-statement
    

    The compound-statement, as per [stmt.block], is, in this context, what OP refers to as "within braces" (block):

    A compound statement (also known as a block) groups a sequence of statements into a single statement.

    compound-statement:
        { statement-seq_opt }
    

    Whereas ctor-initializer, as per [class.base.init], is particularly allowed only for the special kind of functions that are constructors [emphasis mine]:

    In the definition of a constructor for a class, initializers for direct and virtual base class subobjects and non-static data members can be specified by a ctor-initializer, which has the form

    ctor-initializer:
    : mem-initializer-list
    

    With this, we can answer the OP's questions.


    Is member initializer list considered part of the body of a constructor or it it considered part of the declarator

    Yes, as per the above the member initializer, formally mem-initializer-list, is part of the function-body of the constructor.


    My second question is that is the above given description correct?

    1. Ctor definition: This includes the whole

    //this whole thing is ctor definition
    Person(int pAge): age(pAge)
            {
            }
    

    Correct.

    2. Member initializer: This is the age(pAge) part.

    Correct, formally the mem-initializer-list (whereas : age(pAge) is the ctor-initializer

    3. Ctor declaration: This is the Person(int pAge) part.

    Not entirely correct: a definition is also a declaration. [dcl.fct] describe the rules of function declarations, and in simple terms, Person(int pAge); is a declaration that is not a definition, particularly here by omission of a function-body.

    4. Ctor's body: This is the region between the opening { and the closing }.

    Incorrect. The body of a function, as covered above, container also, optionally, a ctor-initializer. In OP's example, : age(pAge) {} is the function-body of the constructor.