<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>c++17 Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/c17/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/c17/</link>
	<description></description>
	<lastBuildDate>Sun, 17 May 2020 21:51:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>c++17 Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/c17/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Python-Like enumerate() In C++17 &#8211; C++ Weekly EP1</title>
		<link>https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/</link>
					<comments>https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 17 May 2020 21:34:20 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[auto]]></category>
		<category><![CDATA[c++17]]></category>
		<category><![CDATA[constexpr]]></category>
		<category><![CDATA[enumerate]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6774</guid>

					<description><![CDATA[<p>[crayon-663a437fba837760141053/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/">Python-Like enumerate() In C++17 &#8211; C++ Weekly EP1</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Python-Like enumerate() In C++17 - C++ Weekly EP1" width="500" height="375" src="https://www.youtube.com/embed/ENpgbTrrebo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;vector&gt;
#include &lt;list&gt;
#include &lt;tuple&gt;
using namespace std;

// Python-Like enumerate() In C++17
// http://reedbeta.com/blog/python-like-enumerate-in-cpp17/
template &lt;typename T,
          typename TIter = decltype(std::begin(std::declval&lt;T&gt;())),
          typename = decltype(std::end(std::declval&lt;T&gt;()))&gt;
constexpr auto enumerate(T &amp;&amp; iterable) {
  struct iterator {
    int i;
    TIter iter;
    bool operator != (const iterator &amp; other) const { return iter != other.iter; }
    void operator ++ () { ++i; ++iter; }
    auto operator * () const { return std::tie(i, *iter); }
  };
  struct iterable_wrapper {
    T iterable;
    auto begin() { return iterator{ 0, std::begin(iterable) }; }
    auto end() { return iterator{ 0, std::end(iterable) }; }
  };
  // return iterable_wrapper{ iterable }; // this makes a copy if iterable is a rvalue.
  return iterable_wrapper{ std::forward&lt;T&gt;(iterable) };  
}

struct A {
  int val;
  A(int val): val(val) { cout &lt;&lt; &quot;A(int)&quot; &lt;&lt; endl; }
  A(const A&amp; a): val(a.val) { cout &lt;&lt; &quot;A(A&amp;)&quot; &lt;&lt; endl; }
  A(A&amp;&amp; a): val(a.val) { cout &lt;&lt; &quot;A(A&amp;&amp;)&quot; &lt;&lt; endl; }    
};

int main(int argc, char** argv) {
  vector&lt;int&gt; arr{1, 2, 4};  
  
  for (size_t i = 0; i &lt; arr.size(); ++i)
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; arr[i] &lt;&lt; endl;

  size_t idx = 0;
  for (int v : arr)
    cout &lt;&lt; idx++ &lt;&lt; &quot; &quot; &lt;&lt; v &lt;&lt; endl;

  for (auto it = begin(arr); it != end(arr); ++it)
    cout &lt;&lt; distance(begin(arr), it) &lt;&lt; &quot; &quot; &lt;&lt; *it &lt;&lt; endl;
  

  for (const auto&amp; [i, v] : enumerate(arr))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v &lt;&lt; endl;

  list&lt;string&gt; lst{&quot;hello&quot;, &quot;world!&quot;};  

  for (const auto&amp; [i, v] : enumerate(lst))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v &lt;&lt; endl;

  for (const auto&amp; [i, v] : enumerate(string(&quot;abcde&quot;)))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v &lt;&lt; endl;

  vector&lt;A&gt; vec;
  vec.reserve(3);
  vec.emplace_back(1);
  vec.emplace_back(2);
  vec.emplace_back(4);

  for (const auto&amp; [i, v] : enumerate(vec))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v.val &lt;&lt; endl;

  for (const auto&amp; [i, v] : enumerate(vector&lt;A&gt;{A(1), A(2), A(4)}))
    cout &lt;&lt; i &lt;&lt; &quot; &quot; &lt;&lt; v.val &lt;&lt; endl;

  return 0;
}</pre>
</div></div>



<p></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/">Python-Like enumerate() In C++17 &#8211; C++ Weekly EP1</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/c/python-like-enumerate-in-c17/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
