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

3

5

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

Crap answered 17/6, 2012 at 20:52 Comment(13)
You really should add the error message this is about.Thumbscrew
This is not C++, but C++/CLI.Baptlsta
@GManNickG: C++/CX, not C++/CLI.Newsboy
Closed as "too localized"? I've seen this same question asked at least a dozen times over the past nine months on MSDN forums and mailing lists in which I participate. How is this in any way "too localized"?Newsboy
@JamesMcNellis thanks for re-opening; I had to look quite a bit to find the answer, and it was kind of scattered through MSDN but would much rather be able to find it on SO.Crap
@JamesMcNellis we have asked for a close because the asker answered his own question at the same minute he asked. PS: I didn't go for "too localized", I went for "other" but you can understand from where too localized come from. It is what i previously saidWickner
@AdelBoutros what's wrong with answering your own question? I thought it was encouraged to share knowledge on the site if either it wasn't already here, or difficult to find elsewhere.Crap
@AdelBoutros meta.stackexchange.com/questions/12513/…Crap
When you answer your question at the same minute. This raises some doubts. Did you really come up with the answer while writing the question? I assure you I am not judging your intentions, I am just expressing my doubts. So please don't take it personal :)Wickner
@AdelBoutros: There is absolutely nothing wrong with asking a question and immediately answering it yourself. Some of the best answers on Stack Overflow (e.g., the excellent post on the copy-and-swap idiom) were posted in this manner. Stack Overflow exists to facilitate the sharing of information.Newsboy
@AdelBoutros I actually did all the research before posting the question since it took me a while to find the answer. I would claim that the fact that SO provides the facility on the same screen to answer your question while asking it means that this is considered "OK", and not something to be second-guessed, wouldn't you?Crap
In that case, I find it more appropriate to be posted on the "meta" insteadWickner
@AdelBoutros Unfortunately I still don't understand your position :( Why would a question about C++/Cx be posted on stackoverflow meta?Crap
C
10

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

Crap answered 17/6, 2012 at 20:52 Comment(3)
Is there an include or using missing, as for_each, begin, and end were all undefined for meSindhi
ah, that is what was left outSindhi
There's a slight syntax error. It should be [&](my_collection_type^ value) { .. }. That is, it should use parenthesis after the initial set of brackets. Also, the ampersand inside the brackets is not required if you're not referencing any external vars outside of the lambda.Ebullition
S
10

This should also work

for (IIterator<T> iter = iterable->First(); iter->HasCurrent; iter->MoveNext())
{
   T item = iter->Current;
}
Sindhi answered 27/5, 2013 at 0:13 Comment(0)
F
4

When writing Windows Runtime Component in C++/CX you can use C#-like construction:

for each (auto item in collection)
{
    item->DoSomething();
}
Flyfish answered 31/8, 2015 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.