{"id":104321,"date":"2020-09-30T07:00:00","date_gmt":"2020-09-30T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104321"},"modified":"2020-09-30T06:36:34","modified_gmt":"2020-09-30T13:36:34","slug":"20200930-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20200930-00\/?p=104321","title":{"rendered":"Bonus operations for C++\/WinRT iterators: The IIterator&lt;T&gt;"},"content":{"rendered":"<p>C++\/WinRT provides iterators for a number of basic Windows Runtime collections. Let&#8217;s start with the lowest-level Windows Runtime iterator-ish thing: The <code>IIterator&lt;T&gt;<\/code>, which represents a cursor in a collection.<\/p>\n<p>C++\/WinRT adds two additional operators to the Windows Runtime-defined methods on the C++\/WinRT <code>IIterator&lt;T&gt;<\/code> interface:<\/p>\n<ul>\n<li>The dereferencing <code>*<\/code> operator fetches the current item.<\/li>\n<li>The preincrement <code>++<\/code> operator moves the iterator to the next item. If the iterator has moved past the last item of the collection, then the iterator is set to <code>nullptr<\/code>.<\/li>\n<\/ul>\n<p>This allows you to write code that consumes the contents of an <code>IIterator<\/code> in a somewhat more idiomatic way.<\/p>\n<pre>for (IIterator&lt;int&gt; it = start.HasCurrent() ? start : nullptr; it; ++it) {\r\n  int value = *it;\r\n  \/* do something with the value *\/\r\n}\r\n<\/pre>\n<p>This is still rather awkward because you have to check whether the starting point is already beyond the end of the collection and convert it to a <code>nullptr<\/code>. A little helper function can help with that.<\/p>\n<pre>template&lt;typename T&gt;\r\nIIterator&lt;T&gt; as_cpp_iterator(IIterator&lt;T&gt; const&amp; it)\r\n{\r\n  return it.HasCurrent() ? it : nullptr;\r\n}\r\n<\/pre>\n<p>This simplifies the <code>for<\/code> loop slightly:<\/p>\n<pre>for (IIterator&lt;int&gt; it = as_cpp_iterator(start); it; ++it) {\r\n  int value = *it;\r\n  \/* do something with the value *\/\r\n}\r\n<\/pre>\n<p>These operators are added primarily for use in C++ standard algorithms that accept input iterators.<\/p>\n<pre>std::vector&lt;int&gt; to_vector(IIterator&lt;int&gt; const&amp; it)\r\n{\r\n  std::vector&lt;int&gt; v;\r\n  std::copy(as_cpp_iterator(it), {}, std::back_inserter(v));\r\n  return v;\r\n}\r\n\r\nint sum(IIterator&lt;int&gt; const&amp; it)\r\n{\r\n    return std::reduce(as_cpp_iterator(it), {}, 0);\r\n}\r\n<\/pre>\n<p>Iterators are a rather clumsy way of walking through a collection. Next time, we&#8217;ll look at something better.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The equivalent of an input iterator.<\/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-104321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The equivalent of an input iterator.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104321","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=104321"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104321\/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=104321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}