{"id":104325,"date":"2020-10-01T07:00:00","date_gmt":"2020-10-01T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104325"},"modified":"2020-10-01T07:22:15","modified_gmt":"2020-10-01T14:22:15","slug":"20201001-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20201001-00\/?p=104325","title":{"rendered":"Bonus operations for C++\/WinRT iterators: The IIterable&lt;T&gt;, and C++\/WinRT collections"},"content":{"rendered":"<p>Last time, we saw that <a title=\"Bonus operations for C++\/WinRT iterators: The IIterator&lt;T&gt;\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200930-00\/?p=104321\"> C++\/WinRT provides a few bonus operators to the <code>IIterator&lt;T&gt;<\/code><\/a> in order to make it work a little bit more smoothly with the C++ standard library.<\/p>\n<p>Today we&#8217;re going to look at <code>IIterable&lt;T&gt;<\/code>, which is an interface that says &#8220;You can get an iterator from me.&#8221;<\/p>\n<p>These Windows Runtime interfaces correspond to analogous concepts in many programming languages.<\/p>\n<table class=\"cp3\" style=\"border-collapse: collapse;\" border=\"1\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<td rowspan=\"2\">C++\/WinRT<\/td>\n<td><tt>IIterable&lt;T&gt;<\/tt><\/td>\n<td><tt>IIterator&lt;T&gt;<\/tt><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\"><tt>iterator = o.First();<\/tt><br \/>\n<tt>for (auto&amp;&amp; v : o) ...<\/tt><\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">C++<\/td>\n<td><tt>begin<\/tt>\/<tt>end<\/tt><\/td>\n<td><tt>iterator<\/tt><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\"><tt>iterator = begin(o);<\/tt><br \/>\n<tt>for (auto&amp;&amp; v : o) ...<\/tt><\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">C#<\/td>\n<td><tt>IEnumerable&lt;T&gt;<\/tt><\/td>\n<td><tt>IEnumerator&lt;T&gt;<\/tt><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\"><tt>enumerator = o.GetEnumerator();<\/tt><br \/>\n<tt>foreach (var v in o) ...<\/tt><\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">Java<\/td>\n<td><tt>Iterable&lt;T&gt;<\/tt><\/td>\n<td><tt>Iterator&lt;T&gt;<\/tt><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\"><tt>iterator = enumerable.iterator();<\/tt><br \/>\n<tt>for (var v : o) ...<\/tt><\/td>\n<\/tr>\n<tr>\n<td rowspan=\"2\">JavaScript<\/td>\n<td><tt>@@iterator<\/tt><\/td>\n<td>(unnamed)<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\"><tt>iterator = o[Symbol.iterator]();<\/tt><br \/>\n<tt>for (v in o) ...<\/tt><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>As I noted in the table above, these iterators are designed primarily for use by ranged for statements.<\/p>\n<pre>for (auto&amp;&amp; value : collection)\r\n{\r\n    \/* do something with value *\/\r\n}\r\n<\/pre>\n<p>They can also be used in more general algorithms:<\/p>\n<pre>std::vector&lt;int&gt; to_vector(IIterable&lt;int&gt; const&amp; collection)\r\n{\r\n  std::vector&lt;int&gt; v;\r\n  std::copy(begin(collection), end(collection), std::back_inserter(v));\r\n  return v;\r\n}\r\n<\/pre>\n<p>Here&#8217;s a peek behind the scenes: For collections which support a <code>GetAt<\/code> method (such as <code>IVector<\/code>, <code>IVectorView<\/code>, and <code>IBindableVector<\/code>), this is implemented by an internal <code>fast_iterator<\/code>, and the expansion of the ranged for loop comes out like this:<\/p>\n<pre>auto&amp;&amp; range = collection;\r\nauto size = range.Size();\r\nfor (uint32_t index = 0; index &lt; size; ++index)\r\n{\r\n  auto&amp;&amp; value = range.GetAt(index);\r\n  \/* do something with value *\/\r\n}\r\n<\/pre>\n<p>The temporary <code>range<\/code> is part of the ranged for statement. There are some pre-existing subtleties here, <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/range-for#Temporary_range_expression\"> which I leave you to learn about<\/a>.<\/p>\n<p>For collections which are not indexable, but which are nevertheless iterable, the code falls back to the traditional <code>Iterator<\/code>-based loop:<\/p>\n<pre>for (auto iterator = as_cpp_iterator(collection.First()); iterator; ++iterator)\r\n{\r\n  auto&amp;&amp; value = *iterator;\r\n  \/* do something with value *\/\r\n}\r\n<\/pre>\n<p>That version takes advantage of iterator overloads we saw last time.<\/p>\n<p>But wait, we&#8217;re not done yet. There&#8217;s a little gotcha here that we&#8217;ll look at next time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Stepping through a collection.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-104325","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Stepping through a collection.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=104325"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104325\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=104325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}