“在成员函数外部的封闭类的定义内需要的默认成员初始化程序" -我的代码格式错误吗? [英] "Default member initializer needed within definition of enclosing class outside of member functions" - is my code ill-formed?

查看:103
本文介绍了“在成员函数外部的封闭类的定义内需要的默认成员初始化程序" -我的代码格式错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <utility>

struct foo
{
    int x{0};
    foo() noexcept = default;
    void f() noexcept(noexcept(std::declval<foo&>())) {}
};

int main()
{ 
}

godbolt上的实时示例

上面的代码可与我测试过的任何版本的g ++以及从3.6到3.9.1的clang ++一起编译,但不能与clang ++ 4.0.0一起编译:

The code above compiles with any version of g++ I tested, and with clang++ from 3.6 to 3.9.1, but does not compile with clang++ 4.0.0:

test.cpp:6:5: error: default member initializer for 'x' needed within 
definition of enclosing class 'foo' outside of member functions
    foo() noexcept = default;
    ^
type_traits:126:26: note: in instantiation of template 
class 'std::is_function<foo &>' requested here
    : public conditional<_B1::value, _B1, __or_<_B2, _B3, _Bn...>>::type
                        ^
type_traits:154:39: note: in instantiation of template 
class 'std::__or_<std::is_function<foo &>,
    std::is_reference<foo &>, std::is_void<foo &> >' requested here
    : public integral_constant<bool, !_Pp::value>
                                    ^
type_traits:598:14: note: in instantiation of template 
class 'std::__not_<std::__or_<std::is_function<foo &>,
    std::is_reference<foo &>, std::is_void<foo &> > >' requested here
    : public __not_<__or_<is_function<_Tp>, is_reference<_Tp>,
            ^
type_traits:121:26: note: in instantiation of template 
class 'std::is_object<foo &>' requested here
    : public conditional<_B1::value, _B1, _B2>::type
                        ^
type_traits:635:14: note: in instantiation of template 
class 'std::__or_<std::is_object<foo &>,
    std::is_reference<foo &> >' requested here
    : public __or_<is_object<_Tp>, is_reference<_Tp>>::type
            ^
type_traits:1667:33: note: in instantiation of template 
class 'std::__is_referenceable<foo &>' requested here
template<typename _Tp, bool = __is_referenceable<_Tp>::value>
                                ^
type_traits:1678:14: note: in instantiation of default 
argument for '__add_rvalue_reference_helper<foo &>'
    required here
    : public __add_rvalue_reference_helper<_Tp>
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type_traits:2267:12: note: in instantiation of template 
class 'std::add_rvalue_reference<foo &>' requested
    here
    inline typename add_rvalue_reference<_Tp>::type
        ^
wtfff.cpp:7:32: note: while substituting explicitly-specified 
template arguments into function template 'declval'
    void f() noexcept(noexcept(std::declval<foo&>())) {}
                            ^
wtfff.cpp:5:9: note: default member initializer declared here
    int x{0};
        ^

我的代码格式不正确吗?如果是,该错误的含义是什么?

Is my code ill-formed? If so, what's the meaning of the error?

请注意,从构造函数中删除noexcept或从x中删除{0}初始化程序将使代码编译.

Note that removing the noexcept from the constructor or the {0} initializer from x will make the code compile.

推荐答案

据我所知,您的代码很好. Clang似乎在= default构造函数上苦苦挣扎,而不仅仅是手动定义默认的构造函数.它的源代码中包含以下内容:

Your code is fine from what I can tell. Clang seems to struggle with the = default constructor rather than just defining a default constructor manually. It has the following spiel in its source code about it:

DR1351: 非静态数据成员的括号或相等初始化程序 调用其类的默认默认构造函数或 将类包含在可能评估的子表达式中, 程序格式错误.

DR1351: If the brace-or-equal-initializer of a non-static data member invokes a defaulted default constructor of its class or of an enclosing class in a potentially evaluated subexpression, the program is ill-formed.

此解决方案不可行: 在未评估的上下文中,可能需要默认构造函数 特别是在noexcept表达式的操作数中,我们可以 无法为封闭的类计算异常规范.

This resolution is unworkable: the exception specification of the default constructor can be needed in an unevaluated context, in particular, in the operand of a noexcept-expression, and we can be unable to compute an exception specification for an enclosed class.

任何尝试解决默认默认值的异常说明的尝试 初始化器在词汇上完成之前的构造函数将最终 来到这里,我们可以对其进行诊断.

Any attempt to resolve the exception specification of a defaulted default constructor before the initializer is lexically complete will ultimately come here at which point we can diagnose it.

我个人认为可能是错误地发现了错误.但是它专门提到了默认的默认构造函数".

I think it may be incorrectly picking up the error, personally. But it specifially mentions "defaulted default constructor".

以下似乎有效:

#include <utility>

struct foo
{
    int x{0};
    foo() noexcept {} // = default;
    void f() noexcept(noexcept(std::declval<foo&>())) {}
};

int main()
{ 
}

这篇关于“在成员函数外部的封闭类的定义内需要的默认成员初始化程序" -我的代码格式错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆