使用非静态数据成员和嵌套类构造函数的类初始化时出错
以下代码非常简单,我预计它应该编译好。
The following code is quite trivial and I expected that it should compile fine.
struct A
{
struct B
{
int i = 0;
};
B b;
A(const B& _b = B())
: b(_b)
{}
};
我用g ++版本4.7.2,4.8.1,clang ++ 3.2和3.3测试了这个代码。除了此代码上的g ++ 4.7.2 segfaults的事实( http://gcc.gnu.org/bugzilla) /show_bug.cgi?id=57770 ),其他测试的编译器给出的错误消息解释不多。
I've tested this code with g++ version 4.7.2, 4.8.1, clang++ 3.2 and 3.3. Apart from fact that g++ 4.7.2 segfaults on this code (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57770), the other tested compilers give error messages that don't explain much.
g ++ 4.8.1:
g++ 4.8.1:
test.cpp: In constructor ‘constexpr A::B::B()’:
test.cpp:3:12: error: constructor required before non-static data member for ‘A::B::i’ has been parsed
struct B
^
test.cpp: At global scope:
test.cpp:11:23: note: synthesized method ‘constexpr A::B::B()’ first required here
A(const B& _b = B())
^
clang ++ 3.2和3.3:
clang++ 3.2 and 3.3:
test.cpp:11:21: error: defaulted default constructor of 'B' cannot be used by non-static data member initializer which appears before end of class definition
A(const B& _b = B())
^
使这个代码可编译是可能的,它应该没有什么区别。有两个选项:
Making this code compilable is possible and seems like it should make no difference. There are two options:
struct B
{
int i = 0;
B(){} // using B()=default; works only for clang++
};
或
struct B
{
int i;
B() : i(0) {} // classic c++98 initialization
};
这个代码真的不正确或者编译器错了吗?
Is this code really incorrect or are the compilers wrong?
这个代码真的不正确或者编译器错了吗?
Is this code really incorrect or are the compilers wrong?
好吧,没有。该标准有一个缺陷 - 它说, A
被认为是完成,而解析初始化为 B :: i
,并且 B :: B()
(它使用 B :: i
的初始化器) A
的定义。这显然是循环的。考虑这个:
Well, neither. The standard has a defect -- it says both that A
is considered complete while parsing the initializer for B::i
, and that B::B()
(which uses the initializer for B::i
) can be used within the definition of A
. That's clearly cyclic. Consider this:
struct A {
struct B {
int i = (A(), 0);
};
A() noexcept(!noexcept(B()));
};
这有一个矛盾: B :: B()
隐含 noexcept
iff A()
不抛出, A
不会抛出iff B :: B()
是不是 noexcept
。这一领域还有许多其他循环和矛盾。
This has a contradiction: B::B()
is implicitly noexcept
iff A()
does not throw, and A()
does not throw iff B::B()
is not noexcept
. There are a number of other cycles and contradictions in this area.
这是由核心问题跟踪1360 和 1397 。请特别注意核心问题1397中的此注释:
This is tracked by core issues 1360 and 1397. Note in particular this note in core issue 1397:
也许解决这个问题的最好方法是,静态数据成员初始化器使用其类的默认构造函数。
Perhaps the best way of addressing this would be to make it ill-formed for a non-static data member initializer to use a defaulted constructor of its class.
这是我在Clang中实现的规则的一种特殊情况解决此问题。 Clang的规则是,在解析该类的非静态数据成员初始化器之前,不能使用类的默认默认构造函数。因此,Clang在这里发布诊断:
That's a special case of the rule that I implemented in Clang to resolve this issue. Clang's rule is that a defaulted default constructor for a class cannot be used before the non-static data member initializers for that class are parsed. Hence Clang issues a diagnostic here:
A(const B& _b = B())
^
...因为Clang在解析默认初始化器之前解析默认参数,并且此默认参数需要 B
的默认初始化器已被解析(为了隐式定义 B :: B()
)。
... because Clang parses default arguments before it parses default initializers, and this default argument would require B
's default initializers to have already been parsed (in order to implicitly define B::B()
).