是否有可能初始化非POD与operator new和初始化器语法定义的数组? [英] Is it possible to initialise an array of non-POD with operator new and initialiser syntax?
问题描述
我刚才已经阅读并理解<一个href=\"http://stackoverflow.com/questions/15183671/is-it-possible-to-initialise-an-array-in-c-11-by-using-new-operator\">Is可以通过使用新的运营商初始化在C ++ 11的阵列,但它并没有完全解决我的问题。
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.
这code给我一个编译错误锵:
This code gives me a compile error in Clang:
struct A
{
A(int first, int second) {}
};
void myFunc()
{
new A[1] {{1, 2}};
}
我的预期{{1,2}}初始化一个单一的元素数组,又与构造函数初始化ARGS {1,2},但我得到这个错误:
I expected {{1, 2}} to initialise the array with a single element, in turn initialised with the constructor args {1, 2}, but I get this error:
error: no matching constructor for initialization of 'A'
new A[1] {{1, 2}};
^
note: candidate constructor not viable: requires 2 arguments, but 0 were provided
A(int first, int second) {}
^
note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
struct A
^
为什么这个语法不行?
Why does this syntax not work?
推荐答案
这似乎是铛++错误15735 一>。声明一个默认的构造函数(使它访问并不会被删除),并在程序编译,即使默认的构造函数不叫:
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 <iostream>
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}};
}
G ++ 4.9还接受OP的程序无需修改。
g++4.9 also accepts the OP's program without modifications.
这篇关于是否有可能初始化非POD与operator new和初始化器语法定义的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!