“Default member initializer needed within definition of enclosing class outside of member functions” - is my code ill-formed?

前端 未结 5 1064
别那么骄傲
别那么骄傲 2020-12-05 02:50
#include 

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

int main()
         


        
5条回答
  •  隐瞒了意图╮
    2020-12-05 03:08

    Your usage is fine.

    • Your int x{0} clearly falls under the category non-static data member with initializer (C++11).
    • The declval does not require a complete type--that's kind of the point of it, as explained in this nice exposition.

    What's left? Compiler bug, I guess. How about this for evidence? Use a complete type z instead of foo in your declval.

    #include 
    struct z{};
    struct foo
    {
        int x{0};
        foo() noexcept = default;
        void f() noexcept( noexcept( std::declval() ) ) {}
    };
    
    int main()
    { 
    }
    

    Clang 4.0.0 on godbolt still errors the same way. Unfortunately I do not have clang 4.0.0 available on a machine to test, so I cannot not say whether it is Clang or godbolt for certain.

提交回复
热议问题