相关疑难解决方法(0)

使用非静态数据成员和嵌套类构造函数的类内初始化时出错

以下代码非常简单,我希望它应该编译好.

struct A
{
    struct B
    {
        int i = 0;
    };

    B b;

    A(const B& _b = B())
        : b(_b)
    {}
};
Run Code Online (Sandbox Code Playgroud)

我用g ++版本4.7.2,4.8.1,clang ++ 3.2和3.3测试了这段代码.除了g ++ 4.7.2对此代码的段错误(http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57770)之外,其他经过测试的编译器提供的错误消息并不能解释太多.

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())
                       ^
Run Code Online (Sandbox Code Playgroud)

clang ++ 3.2和3.3:

test.cpp:11:21: error: defaulted …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer c++11

88
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

c++11 ×1

language-lawyer ×1