There are still some people maintaining code bases written in C++/CX, even though C++/WinRT is the new hotness. Suppose you have a reference to a Windows Runtime vector in C++/CX, either an IVector<T>^
or an IVectorÂView<T>^
, and you want to clone it so that you can operate on the clone without affecting the original.
You could create a vector and copy the items across:
IVector<Thing^>^ original = GetTheThings(); Vector<Thing^> vec = ref new Vector<Thing^>(); for (auto&& thing : original) { vec->Append(thing); }
You can make the Vector
run the loop by using the constructor overload that takes a pair of iterators.
IVector<Thing^>^ original = GetTheThings(); Vector<Thing^> vec = ref new Vector<Thing^>(begin(original), end(original));
Even more directly, you could slurp the entire collection into a std::vector
and then move the std::vector
into a new Platform::
Collections::
Vector
.
IVector<Thing^>^ original = GetTheThings(); Vector<Thing^> vec = ref new Vector<Thing^>(to_vector(original));
I still like the C#’s simple clone mechanism:
var clone = input.ToList();
Nevertheless this syntax seems to be better than C++/WinRT one:
https://devblogs.microsoft.com/oldnewthing/20191122-00/?p=103123