c++ - C++17 中 protected 构造函数的规则改变了吗?

标签 c++ constructor language-lawyer c++17 protected

我有这个测试用例:

struct A{ protected: A(){} };
struct B: A{};
struct C: A{ C(){} };
struct D: A{ D() = default; };

int main(){
    (void)B{};
    (void)C{};
    (void)D{};
}

gcc 和 clang 都在 C++11 和 C++14 模式下编译它。两者都在 C++17 模式下失败:

$ clang++ -std=c++17 main.cpp 
main.cpp:7:10: error: base class 'A' has protected default constructor
        (void)B{};
                ^
main.cpp:1:22: note: declared protected here
struct A{ protected: A(){} };
                     ^
main.cpp:9:10: error: base class 'A' has protected default constructor
        (void)D{};
                ^
main.cpp:1:22: note: declared protected here
struct A{ protected: A(){} };
                     ^
2 errors generated.

$ clang++ --version
clang version 6.0.0 (http://llvm.org/git/clang.git 96c9689f478d292390b76efcea35d87cbad3f44d) (http://llvm.org/git/llvm.git 360f53a441902d19ce27d070ad028005bc323e61)
Target: x86_64-unknown-linux-gnu
Thread model: posix

(clang 编译自 master 分支 2017-12-05。)

$ g++ -std=c++17 main.cpp 
main.cpp: In function 'int main()':
main.cpp:7:10: error: 'A::A()' is protected within this context
  (void)B{};
          ^
main.cpp:1:22: note: declared protected here
 struct A{ protected: A(){} };
                      ^
main.cpp:9:10: error: 'A::A()' is protected within this context
  (void)D{};
          ^
main.cpp:1:22: note: declared protected here
 struct A{ protected: A(){} };
                      ^

$ g++ --version
g++ (GCC) 8.0.0 20171201 (experimental)
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

这种行为变化是 C++17 的一部分还是两个编译器中的错误?

最佳答案

aggregate 的定义自 C++17 以来发生了变化。

C++17 之前

no base classes

C++17 起

no virtual, private, or protected (since C++17) base classes

这意味着,对于 BD,它们在 C++17 之前不是聚合类型,然后对于 B{}D{}, value-initialization将执行,然后 defaulted default constructor将被调用;这很好,因为基类的 protected 构造函数可以被派生类的构造函数调用。

自 C++17 起,BD 成为聚合类型(因为它们只有 public 基类,注意对于类D,从 C++11 开始允许聚合类型使用显式默认的默认构造函数),然后对于 B{}D{}aggregate-initialization将执行,

Each direct public base, (since C++17) array element, or non-static class member, in order of array subscript/appearance in the class definition, is copy-initialized from the corresponding clause of the initializer list.

If the number of initializer clauses is less than the number of members and bases (since C++17) or initializer list is completely empty, the remaining members and bases (since C++17) are initialized by their default initializers, if provided in the class definition, and otherwise (since C++14) by empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates). If a member of a reference type is one of these remaining members, the program is ill-formed.

即基类子对象直接进行值初始化,绕过BD的构造函数;但是A的默认构造函数是protected,则代码失败。 (注意 A 不是聚合类型,因为它有一个用户提供的构造函数。)

顺便说一句:C(带有用户提供的构造函数)在 C++17 之前和之后都不是聚合类型,所以这两种情况都可以。

关于c++ - C++17 中 protected 构造函数的规则改变了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47656093/

相关文章:

c++ - [over.unary]/2中注释中提到的隐藏的编译示例

c++ - (作业)尝试用 C++ 制作 Yahtzee 游戏

c++ - 在几何中使用 boost rtree 查找结果点

c++ - gcc 原子内置函数这么慢是正常的吗?

C++如何调用模板化构造函数

c++ - 在使用 new (c++) 的构造函数调用中不使用括号

c++ - 空终止字符数组(c 字符串)没有 for 循环不打印

c++ - 从匹配返回值类型的映射中返回值

c++ - 聚合初始化不支持构造函数访问

c++ - 与 auto_ptr 声明不同,unique_ptr 声明是否在其模板类型为不完整类型时是明确定义的?