更改了 C++17 中受保护构造函数的规则?
Changed rules for protected constructors in C++17?
我有这个测试用例:
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 Branch 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 的一部分还是两个编译器中的错误?
在 C++17 中,有关聚合的规则发生了变化。
例如,您现在可以在 C++17 中执行此操作:
struct A { int a; };
struct B { B(int){} };
struct C : A {};
struct D : B {};
int main() {
(void) C{2};
(void) D{1};
}
请注意,我们没有继承构造函数。在 C++17 中,C
和 D
现在是聚合,即使它们具有基数 classes。
在 {}
中,聚合初始化开始,不发送任何参数将被解释为与从外部调用父级的默认构造函数相同。
例如,可以通过将 class D
更改为以下内容来禁用聚合初始化:
struct B { protected: B(){} };
struct D : B {
int b;
private:
int c;
};
int main() {
(void) D{}; // works!
}
这是因为当成员具有不同的访问说明符时聚合初始化不适用。
with = default
起作用的原因是因为它不是用户提供的构造函数。更多信息,请访问 。
自 C++17 以来,aggregate 的定义发生了变化。
C++17 之前
no base classes
自 C++17 起
no virtual, private, or protected (since C++17)
base classes
这意味着,对于B
和D
,它们在C++17之前不是聚合类型,然后对于B{}
和D{}
,value-initialization will be performed, then the defaulted default constructor 将被调用;这很好,因为基 class 的 protected
构造函数可以被派生 class 的构造函数调用。
自 C++17 起,B
和 D
成为聚合类型(因为它们只有 public
基数 class,请注意对于 class 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.
这意味着基础class子对象将被直接值初始化,绕过B
和D
的构造函数;但 A
的默认构造函数是 protected
,则代码失败。 (注意 A
不是聚合类型,因为它有一个用户提供的构造函数。)
顺便说一句:C
(使用用户提供的构造函数)在 C++17 之前和之后都不是聚合类型,因此这两种情况都可以。
我有这个测试用例:
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 Branch 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 的一部分还是两个编译器中的错误?
在 C++17 中,有关聚合的规则发生了变化。
例如,您现在可以在 C++17 中执行此操作:
struct A { int a; };
struct B { B(int){} };
struct C : A {};
struct D : B {};
int main() {
(void) C{2};
(void) D{1};
}
请注意,我们没有继承构造函数。在 C++17 中,C
和 D
现在是聚合,即使它们具有基数 classes。
在 {}
中,聚合初始化开始,不发送任何参数将被解释为与从外部调用父级的默认构造函数相同。
例如,可以通过将 class D
更改为以下内容来禁用聚合初始化:
struct B { protected: B(){} };
struct D : B {
int b;
private:
int c;
};
int main() {
(void) D{}; // works!
}
这是因为当成员具有不同的访问说明符时聚合初始化不适用。
with = default
起作用的原因是因为它不是用户提供的构造函数。更多信息,请访问
自 C++17 以来,aggregate 的定义发生了变化。
C++17 之前
no base classes
自 C++17 起
no
virtual, private, or protected (since C++17)
base classes
这意味着,对于B
和D
,它们在C++17之前不是聚合类型,然后对于B{}
和D{}
,value-initialization will be performed, then the defaulted default constructor 将被调用;这很好,因为基 class 的 protected
构造函数可以被派生 class 的构造函数调用。
自 C++17 起,B
和 D
成为聚合类型(因为它们只有 public
基数 class,请注意对于 class 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 membersand bases (since C++17)
are initializedby 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.
这意味着基础class子对象将被直接值初始化,绕过B
和D
的构造函数;但 A
的默认构造函数是 protected
,则代码失败。 (注意 A
不是聚合类型,因为它有一个用户提供的构造函数。)
顺便说一句:C
(使用用户提供的构造函数)在 C++17 之前和之后都不是聚合类型,因此这两种情况都可以。