Why does gcc allow a const object without a user-declared default constructor but not clang?

后端 未结 1 1041
灰色年华
灰色年华 2020-12-10 14:28

Recently Why does a const object requires a user-provided default constructor? was marked a duplicate of Why does C++ require a user-provided default constructor to default-

相关标签:
1条回答
  • 2020-12-10 14:42

    The spec currently requires user-provided default constructors but it appears that GCC is implementing a change based on DR 253 which says that if all sub-objects would be initialized without a user provided default constructor then a user-provided default constructor is not required.

    This change is only draft status, has not been accepted yet and is not part of the standard. So I think think this is behavior intended by GCC developers but I'm not sure if this is a conforming extension though.

    Here's a change to the first example which causes GCC to produce an error:

    class A {
    public:
        void f() {}
    
        int i;
    };
    
    int main()
    {
        A a;       // OK
        const A b; // ERROR
    
        a.f();
        return 0;
    }
    

    Note that gcc downgrades the error to a warning with the -fpermissive flag.

    https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42844

    0 讨论(0)
提交回复
热议问题