(This could also be phrased as "How do I iterate over a collection returned from a C# Windows Runtime Component in C++/CX?")
I tried to use std::for_each
on an IIterable<T>
but get the following compile-time error
error C2664: 'std::begin' : cannot convert parameter 1 from 'my_collection_type ^' to 'Platform::String ^' No user-defined-conversion operator available, or Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
How can I iterate over the collection?
This should also work
for (IIterator<T> iter = iterable->First(); iter->HasCurrent; iter->MoveNext())
{
T item = iter->Current;
}
For this to work, you need to add
#include "collection.h"
(and optionally)
using namespace Windows::Foundation:Collections
to your source file.
You can then iterate over the collection as follows
for_each (begin(my_collection),
end(my_collection),
[&](my_collection_type^ value) {
// code goes here...
});
Note: you might also need using namespace std
(for the for_each
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With