具有空内存初始化器列表和空主体的构造函数

Constructor with an empty mem-initializer-list and an empty body

我在哪里可以找到标准 (C++14) 中的一个子句,该子句声明具有空 mem-initializer-list 和空主体的用户定义的默认构造函数为每个基 class 和每个成员子对象调用默认构造函数。例如考虑下面的代码:

#include <iostream>
class A{
public:
    A() {std::cout << "A" << '\n'; }
};

class Base{
public:
    Base() { std::cout << "Base" << '\n'; }
};

class Derived : public Base {
    A a;
public:
    Derived() {}
};

int main()
{
    Derived d;
}

BaseA 的构造函数均由用户声明的构造函数 Derived() 使用空内存初始化器列表和空主体调用。

问:您具体指的是哪个版本的 C++ 14 标准?

来自 n4296:

https://isocpp.org/std/the-standard

Note:

In some circumstances, C++ implementations implicitly define the default constructor (12.1), copy constructor (12.8), move constructor (12.8), copy assignment operator (12.8), move assignment operator (12.8), or destructor (12.4) member functions. —end note

还有;

A default constructor for a class X is a constructor of class X that either has no parameters or else each parameter that is not a function parameter pack has a default argument. If there is no user-declared constructor for class X, a constructor having no parameters is implicitly declared as defaulted (8.4). An implicitly-declared default constructor is an inline public member of its class.

引用标准草案N4527 12.6.2/9 初始化基和成员[class.base.init]强调我的):

In a non-delegating constructor, if a given potentially constructed subobject is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer), then

(9.1) — if the entity is a non-static data member that has a brace-or-equal-initializer and either

(9.1.1) — the constructor’s class is a union (9.5), and no other variant member of that union is designated by a mem-initializer-id or

(9.1.2) — the constructor’s class is not a union, and, if the entity is a member of an anonymous union, no other member of that union is designated by a mem-initializer-id, the entity is initialized as specified in 8.5;

(9.2) — otherwise, if the entity is an anonymous union or a variant member (9.5), no initialization is performed;

(9.3) — otherwise, the entity is default-initialized (8.5).

注意: 正如 @Howard Hinnant 在下面的评论中指出的那样,在 C++14 (N4141) "finalized" 标准中,上面的引用位于段落中8 而不是第 9 段。