6

Can someone explain why the following code won't compile (formatted oddly to make it a touch easier to see the problem):

ListView ^ listview = gcnew ListView();
listview->Items->AddRange( gcnew array<ListViewItem^> { 
    gcnew ListViewItem( gcnew array<String^> { L"red", L"fish" } ), 
    gcnew ListViewItem( gcnew array<String^> { L"green", L"eggs" } ) 
});

This gives a compile error of

error C2440: 'initializing' : cannot convert from 'const wchar_t[4]' to 'System::Windows::Forms::ListViewItem ^'

If the code is broken into two lines as follows, then all is well:

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 
});

Ignoring why someone wants to make a monolithic one-liner to populate a ListView, why does the compiler have trouble instatiating the ListViewItems in the original code, and how would such a one liner be written?

1 Answer 1

4

This quacks loudly like a compiler parser bug. It gets a bit more interesting if you leave the string array initializer empty. Then you get this description in the Output window:

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

Note the message "ListViemItem^ is not an array or class". This strongly suggests the compiler is applying the initializer to the ListViewItem instead of the string array, that's nonsense. It implodes from there.

This isn't going to get fixed any time soon, if at all. You know the fugly workaround. You can post to connect.microsoft.com for a second opinion.

Sign up to request clarification or add additional context in comments.

2 Comments

Really, it ought to get fixed when support for C++0x aggregate syntax is added. It would have been better if Microsoft hadn't struck out on their own and tried to make this work without a consensus from ISO C++.
Okay, it's good that I'm not missing something silly. I've taken the advice and posted at microsoft's bug reporting site and will update here if anything noteworthy materializes. connect.microsoft.com/VisualStudio/feedback/details/650984/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.