Is it possible to initialise an array of non-POD with operator new and initialiser syntax?

前端 未结 1 879
离开以前
离开以前 2020-12-19 02:43

I have just read and understood Is it possible to initialise an array in C++ 11 by using new operator, but it does not quite solve my problem.

This code gives me a c

1条回答
  •  春和景丽
    2020-12-19 03:26

    This seems to be clang++ bug 15735. Declare a default constructor (making it accessible and not deleted) and the program compiles, even though the default constructor is not called:

    #include 
    
    struct A
    {
       A() { std::cout << "huh?\n"; } // or without definition, linker won't complain
       A(int first, int second) { std::cout << "works fine?\n"; }
    };
    int main()
    {
       new A[1] {{1, 2}};
    }
    

    Live example

    g++4.9 also accepts the OP's program without modifications.

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