Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I iterate over an IIterable<T> in C++/CX?

(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?

like image 950
Jedidja Avatar asked Jun 17 '12 20:06

Jedidja


2 Answers

This should also work

for (IIterator<T> iter = iterable->First(); iter->HasCurrent; iter->MoveNext())
{
   T item = iter->Current;
}
like image 63
The Grand User Avatar answered Sep 29 '22 13:09

The Grand User


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).

like image 33
Jedidja Avatar answered Sep 29 '22 11:09

Jedidja