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?

Jedidja
  • 15,384
  • 16
  • 71
  • 109
  • 1
    You really should add the error message this is about. – sth Jun 17 '12 at 21:10
  • This is not C++, but C++/CLI. – GManNickG Jun 17 '12 at 21:12
  • 2
    @GManNickG: C++/CX, not C++/CLI. – James McNellis Jun 17 '12 at 21:16
  • @JamesMcNellis: Oh, WinRT. Thanks. – GManNickG Jun 17 '12 at 21:18
  • 1
    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"? – James McNellis Jun 18 '12 at 17:10
  • @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. – Jedidja Jun 18 '12 at 21:17
  • @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 said – Adel Boutros Jun 19 '12 at 10:30
  • @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. – Jedidja Jun 19 '12 at 12:45
  • @AdelBoutros http://meta.stackexchange.com/questions/12513/should-i-not-answer-my-own-questions – Jedidja Jun 19 '12 at 12:47
  • 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 :) – Adel Boutros Jun 19 '12 at 13:53
  • 1
    @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](http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom)) were posted in this manner. Stack Overflow exists to facilitate the sharing of information. – James McNellis Jun 19 '12 at 17:06
  • @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? – Jedidja Jun 20 '12 at 11:32
  • In that case, I find it more appropriate to be posted on the "meta" instead – Adel Boutros Jun 20 '12 at 12:25
  • 1
    @AdelBoutros Unfortunately I still don't understand your position :( Why would a question about C++/Cx be posted on stackoverflow meta? – Jedidja Jun 23 '12 at 14:46

3 Answers3

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

legends2k
  • 27,643
  • 22
  • 108
  • 196
Jedidja
  • 15,384
  • 16
  • 71
  • 109
  • Is there an include or using missing, as for_each, begin, and end were all undefined for me – The Grand User May 27 '13 at 00:14
  • ah, that is what was left out – The Grand User Jun 01 '13 at 22:07
  • 1
    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. – Weston Dec 11 '14 at 17:20
9

This should also work

for (IIterator<T> iter = iterable->First(); iter->HasCurrent; iter->MoveNext())
{
   T item = iter->Current;
}
The Grand User
  • 717
  • 4
  • 13
4

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

for each (auto item in collection)
{
    item->DoSomething();
}
Brig Ader
  • 791
  • 6
  • 7