<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>cpp — Little Step</title>
    <link>https://rex.writeas.com/tag:cpp</link>
    <description></description>
    <pubDate>Mon, 16 Nov 2026 00:32:15 +0000</pubDate>
    <item>
      <title>Use std::enable\shared\from\_this for this in smart pointer mode</title>
      <link>https://rex.writeas.com/use-std-enablesharedfrom_this-for-this-in-smart-point-mode?pk_campaign=rss-feed</link>
      <description>As we all know that there is a this pointer for all objects, but if we want to use smart pointer, how to we use. It turns out that it&#39;s n...</description>
      <content:encoded><![CDATA[<p>As we all know that there is a <strong>this</strong> pointer for all objects, but if we want to use smart pointer, how to we use.</p>

<p>It turns out that it&#39;s not that easy by creating a share_ptr from this pointer, since we may have many shared_ptrs points to the same object with without knowing each other.</p>

<p>Taking this code as an example, if we create a share_ptr by dangerous function, we will have two share_ptrs pointing to the same object. Both sp1 and sp2 point to same object, and when the two exits it leads problem.</p>

<pre><code class="language-cpp">struct S
{
  shared_ptr&lt;S&gt; dangerous() {
    return shared_ptr&lt;S&gt;(this);  // don&#39;t do this!
  }
};

int main() {
  shared_ptr&lt;S&gt; sp1(new S);
  shared_ptr&lt;S&gt; sp2 = sp-&gt;dangerous();
  return 0;
}
</code></pre>

<p>How to fix this problem, use the <em>std::enable_shared_from_this</em> help class defined in, it is introduced in C++11.</p>

<pre><code class="language-cpp">struct S: enable_shared_from_this&lt;S&gt; {
  shared_ptr&lt;S&gt; dangerous() {
    return shared_from_this();
  }
};

int main() {
  shared_ptr&lt;S&gt; sp1(new S);
  shared_ptr&lt;S&gt; sp2 = sp-&gt;dangerous();  // not dangerous

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

<p><a href="https://rex.writeas.com/tag:cpp" class="hashtag" rel="nofollow ugc"><span>#</span><span class="p-category">cpp</span></a> <a href="https://rex.writeas.com/tag:share" class="hashtag" rel="nofollow ugc"><span>#</span><span class="p-category">share</span></a>_ptr</p>
]]></content:encoded>
      <guid>https://rex.writeas.com/use-std-enablesharedfrom_this-for-this-in-smart-point-mode</guid>
      <pubDate>Wed, 24 Aug 2022 08:09:16 +0000</pubDate>
    </item>
    <item>
      <title>C++ alternative operators</title>
      <link>https://rex.writeas.com/c-alternative-operators?pk_campaign=rss-feed</link>
      <description>As a C++ programmer for many years, I never noticed the other style of logic operators in C++ until today. When I review part of code wit...</description>
      <content:encoded><![CDATA[<p>As a C++ programmer for many years, I never noticed the other style of logic operators in C++ until today.</p>

<p>When I review part of code witten by one of my colleague which is a mainly a Java programmer, and find he uses <strong>and</strong> other than <strong>&amp;&amp;</strong> in if statement.</p>

<p>And I check cppreference, and find the <a href="https://en.cppreference.com/w/cpp/language/operator_alternative" rel="nofollow ugc">C++ operator alternative</a>.</p>

<table>
<thead>
<tr>
<th>Primary</th>
<th>Alternative</th>
</tr>
</thead>

<tbody>
<tr>
<td>&amp;&amp;</td>
<td>and</td>
</tr>

<tr>
<td>&amp;=</td>
<td>and_eq</td>
</tr>

<tr>
<td>&amp;</td>
<td>bitand</td>
</tr>

<tr>
<td>|</td>
<td>bitor</td>
</tr>

<tr>
<td>~</td>
<td>compl</td>
</tr>

<tr>
<td>!</td>
<td>not</td>
</tr>

<tr>
<td>!=</td>
<td>not_eq</td>
</tr>

<tr>
<td>||</td>
<td>or</td>
</tr>

<tr>
<td>|=</td>
<td>or_eq</td>
</tr>

<tr>
<td>^</td>
<td>xor</td>
</tr>

<tr>
<td>^=</td>
<td>xor_eq</td>
</tr>

<tr>
<td>{</td>
<td>&lt;%</td>
</tr>

<tr>
<td>}</td>
<td>%&gt;</td>
</tr>

<tr>
<td>[</td>
<td>&lt;:</td>
</tr>

<tr>
<td>]</td>
<td>:&gt;</td>
</tr>

<tr>
<td>#</td>
<td>%:</td>
</tr>

<tr>
<td>##</td>
<td>%:%:</td>
</tr>
</tbody>
</table>

<p>The reason for alternative operators is that the source code maybe not in ASCII set, and don&#39;t have the proper operators.</p>

<p>#cpp <a href="https://rex.writeas.com/tag:programming" class="hashtag" rel="nofollow ugc"><span>#</span><span class="p-category">programming</span></a> <a href="https://rex.writeas.com/tag:operator" class="hashtag" rel="nofollow ugc"><span>#</span><span class="p-category">operator</span></a></p>
]]></content:encoded>
      <guid>https://rex.writeas.com/c-alternative-operators</guid>
      <pubDate>Fri, 10 Jan 2020 02:24:48 +0000</pubDate>
    </item>
  </channel>
</rss>