Compiler construction C++/CLI数组初始值设定项编译错误

Compiler construction C++/CLI数组初始值设定项编译错误,compiler-construction,c++-cli,Compiler Construction,C++ Cli,有人能解释一下为什么下面的代码无法编译(格式很奇怪,以便更容易看到问题): ListView^ListView=gcnew ListView(); listview->Items->AddRange(新数组{ gcnew ListViewItem(gcnew数组{L“red”,L“fish”}), gcnew ListViewItem(gcnew数组{L“绿色”,L“鸡蛋”}) }); 这将导致编译错误为 错误C2440:“正在初始化”:无法从“const wchar\u t[4]”转换为“S

有人能解释一下为什么下面的代码无法编译(格式很奇怪,以便更容易看到问题):

ListView^ListView=gcnew ListView();
listview->Items->AddRange(新数组{
gcnew ListViewItem(gcnew数组{L“red”,L“fish”}),
gcnew ListViewItem(gcnew数组{L“绿色”,L“鸡蛋”})
});
这将导致编译错误为

错误C2440:“正在初始化”:无法从“const wchar\u t[4]”转换为“System::Windows::Forms::ListViewItem ^”

如果代码分为以下两行,则一切正常:

ListView^ listview = gcnew ListView();
ListViewItem^ lvi1 = gcnew ListViewItem( gcnew array<String^> { L"red", L"fish" } );
ListViewItem^ lvi2 = gcnew ListViewItem( gcnew array<String^> { L"green", L"eggs" } );
listview->Items->AddRange( gcnew array<ListViewItem^> { 
    lvi1, 
    lvi2 
});
ListView^ListView=gcnew ListView();
ListViewItem ^lvi1=gcnew ListViewItem(gcnew数组{L“red”,L“fish”});
ListViewItem ^lvi2=gcnew ListViewItem(gcnew数组{L“绿色”,L“鸡蛋”});
listview->Items->AddRange(新数组{
lvi1,
lvi2
});

忽略为什么有人想制作一个单行程序来填充ListView,为什么编译器在原始代码中安装ListViewItems时遇到问题,以及如何编写这样一个单行程序

这像编译器解析器bug一样大声嘎嘎作响。如果将字符串数组初始值设定项保留为空,则会更有趣。然后在输出窗口中获得以下描述:

1>c:\projects\cpptemp26\Form1.h(77) : error C2552: '$S4' : non-aggregates cannot be initialized with initializer list
1>        'System::Windows::Forms::ListViewItem ^' is not an array or class : Types which are not array or class types are not aggregate
1>c:\projects\cpptemp26\Form1.h(78) : error C2440: 'initializing' : cannot convert from 'const wchar_t [6]' to 'System::Windows::Forms::ListViewItem ^'
1>        Reason: cannot convert from 'const wchar_t *' to 'System::Windows::Forms::ListViewItem ^'
1>        No user-defined-conversion operator available, or
1>        Cannot convert an unmanaged type to a managed type
注意消息“ListViemItem^不是数组或类”。这强烈表明编译器正在将初始值设定项应用于ListViewItem,而不是字符串数组,这是胡说八道。它从那里内爆


这不会很快得到解决,如果有的话。你知道这个令人讨厌的解决办法。您可以发布到connect.microsoft.com以获取第二个意见。

实际上,当添加了对C++0x聚合语法的支持时,它应该得到修复。如果微软没有自己动手,试图在没有ISO C++的一致性的情况下完成这项工作,那就更好了。好吧,我没有错过一些愚蠢的事情是好的。我已经采纳了这个建议并发布在微软的bug报告网站上,如果有任何值得注意的事情发生,我将在这里更新。
1>c:\projects\cpptemp26\Form1.h(77) : error C2552: '$S4' : non-aggregates cannot be initialized with initializer list
1>        'System::Windows::Forms::ListViewItem ^' is not an array or class : Types which are not array or class types are not aggregate
1>c:\projects\cpptemp26\Form1.h(78) : error C2440: 'initializing' : cannot convert from 'const wchar_t [6]' to 'System::Windows::Forms::ListViewItem ^'
1>        Reason: cannot convert from 'const wchar_t *' to 'System::Windows::Forms::ListViewItem ^'
1>        No user-defined-conversion operator available, or
1>        Cannot convert an unmanaged type to a managed type