<?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++ Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/category/c/</link>
	<description></description>
	<lastBuildDate>Thu, 11 Jun 2020 00:59:04 +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++ Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/category/c/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>C++ 11 Smart Pointers 智能指针</title>
		<link>https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/</link>
					<comments>https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 11 Jun 2020 00:55:32 +0000</pubDate>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[smart pointers]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6896</guid>

					<description><![CDATA[<p>Example code [crayon-663c4665214af271596728/] Output [crayon-663c4665214b3818003224/]</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/">C++ 11 Smart Pointers 智能指针</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="Smart Pointers 智能指针 - C++ Weekly EP3" width="500" height="375" src="https://www.youtube.com/embed/KQt3IjGdqL8?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>Example code</p>



<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;memory&gt;

class Entity {
public:
  Entity() { puts(&quot;Entity created!&quot;); }
  ~Entity() { puts(&quot;Entity destroyed!&quot;); }
};

void ex1() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex1&quot;);
  {
    puts(&quot;Entering ex1::scope1&quot;);
    auto e1 = std::make_unique&lt;Entity&gt;();    
    puts(&quot;Leaving ex1::scope1&quot;);
  }
  puts(&quot;Leaving ex1&quot;);
}

void foo(std::unique_ptr&lt;Entity&gt;) {
  puts(&quot;Entering foo&quot;);
  puts(&quot;Leaving foo&quot;);
}

void ex2() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex2&quot;);
  auto e1 = std::make_unique&lt;Entity&gt;();  
  foo(std::move(e1));
  // e1 was destoried.
  puts(&quot;Leaving ex2&quot;);
}

void ex3() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex3&quot;);
  auto e1 = std::make_shared&lt;Entity&gt;();
  std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
  {
    puts(&quot;Entering ex3::scope1&quot;);
    auto e2 = e1; // use_count ++
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    auto e3 = std::move(e2); // use_count remains
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    puts(&quot;Leaving ex3::scope1&quot;);
  }
  std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
  puts(&quot;Leaving ex3&quot;);
}

void observe(std::weak_ptr&lt;Entity&gt; ew) {
  if (std::shared_ptr&lt;Entity&gt; spt = ew.lock()) {
    std::cout &lt;&lt; spt.use_count() &lt;&lt; std::endl;
    std::cout &lt;&lt; &quot;entity still alive!&quot; &lt;&lt; std::endl;
  } else {
    std::cout &lt;&lt; &quot;entity was expired :(&quot; &lt;&lt; std::endl;
  }
}

void ex4() {
  puts(&quot;--------&quot;);
  puts(&quot;Entering ex4&quot;);
  std::weak_ptr&lt;Entity&gt; ew;  
  {
    puts(&quot;Entering ex4::scope1&quot;);
    auto e1 = std::make_shared&lt;Entity&gt;();
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    ew = e1; // use_count remains
    std::cout &lt;&lt; e1.use_count() &lt;&lt; std::endl;
    observe(ew);
    puts(&quot;Leaving ex4::scope1&quot;);
  }
  observe(ew);
  puts(&quot;Leaving ex4&quot;);
}

int main(int argc, char** argv) {
  ex1();
  ex2();
  ex3();
  ex4();
  return 0;
}</pre>
</div></div>



<h2>Output</h2>



<pre class="crayon-plain-tag">--------
Entering ex1
Entering ex1::scope1
Entity created!
Leaving ex1::scope1
Entity destroyed!
Leaving ex1
--------
Entering ex2
Entity created!
Entering foo
Leaving foo
Entity destroyed!
Leaving ex2
--------
Entering ex3
Entity created!
1
Entering ex3::scope1
2
2
Leaving ex3::scope1
1
Leaving ex3
Entity destroyed!
--------
Entering ex4
Entering ex4::scope1
Entity created!
1
1
2
entity still alive!
Leaving ex4::scope1
Entity destroyed!
entity was expired :(
Leaving ex4</pre>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/c/cpp-11-smart-pointers/">C++ 11 Smart Pointers 智能指针</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/cpp-11-smart-pointers/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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-663c466521964258663405/]</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>
