C++/CLI: Boxing and Generic Lists

.net, boxing, c++-cli, generics

Solution

It is a limit of the .Net generic, which only takes a CLI complient type such as a value type or a reference to a reference type. It does not take C++/CLI-specific types like stack semantics for ref types (which compiles into deterministic finalization) or, in you case, a reference to a boxed value type.

Array is native to CLI and does not have this restriction.

Problem

I am trying to create a generic list of references to `PointF` objects. (No, I am not looking to create a generic list of `PointF` objects.) However, the following line fails to compile: ``` Generic::List<PointF^> ^pointList; // Generates error C3225 ``` On the other hand, creating an array of `PointF` references works without a problem as follows: ``` array<PointF^> ^points = gcnew array<PointF^>; ``` Here is a sample program: ``` using namespace System; using namespace System::Drawing; namespace Generic = System::Collections::Generic; int main(array<System::String ^> ^args) { array<PointF^> ^points = gcnew array<PointF^>{ nullptr, PointF(0.0f, 0.0f), PointF(1.0f, 0.0f), nullptr }; Generic::List<PointF^> ^pointList; Console::WriteLine(L"Hello World"); return 0; } ``` How do I create a generic list of `PointF` references? In other words, how do I create a generic list of boxed `PointF`s?

Original source