<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>C++ on Cheat Sheet</title><link>/tags/c++/</link><description>Recent content in C++ on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/c++/index.xml" rel="self" type="application/rss+xml"/><item><title>Error Handling</title><link>/programming/cpp/advanced/error-handling/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/advanced/error-handling/</guid><description>&lt;h1 id="error-handling-in-c-abseil-status-statusort--gorust-comparison"&gt;Error Handling in C++: Abseil &lt;code&gt;Status&lt;/code&gt;, &lt;code&gt;StatusOr&amp;lt;T&amp;gt;&lt;/code&gt; &amp;amp; Go/Rust Comparison&lt;a class="anchor" href="#error-handling-in-c-abseil-status-statusort--gorust-comparison"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;This article explains why large-scale C++ codebases favor &lt;strong&gt;value-based error handling (Errors as Values)&lt;/strong&gt; over C++ exceptions (&lt;code&gt;try-catch&lt;/code&gt;), how to use open-source &lt;strong&gt;Abseil (&lt;code&gt;abseil-cpp&lt;/code&gt;)&lt;/strong&gt; types &lt;code&gt;absl::Status&lt;/code&gt; and &lt;code&gt;absl::StatusOr&amp;lt;T&amp;gt;&lt;/code&gt;, and how this design maps directly to &lt;strong&gt;Go&amp;rsquo;s &lt;code&gt;(T, error)&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;Rust&amp;rsquo;s &lt;code&gt;Result&amp;lt;T, E&amp;gt;&lt;/code&gt;&lt;/strong&gt;, and &lt;strong&gt;C++23&amp;rsquo;s &lt;code&gt;std::expected&amp;lt;T, E&amp;gt;&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-why-large-scale-c-projects-avoid-exceptions-try-catch"&gt;1. Why Large-Scale C++ Projects Avoid Exceptions (&lt;code&gt;try-catch&lt;/code&gt;)&lt;a class="anchor" href="#1-why-large-scale-c-projects-avoid-exceptions-try-catch"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Although standard C++ supports &lt;code&gt;throw&lt;/code&gt; and &lt;code&gt;try-catch&lt;/code&gt;, many large production C++ codebases (following the &lt;a href="https://google.github.io/styleguide/cppguide.html#Exceptions"&gt;Google C++ Style Guide&lt;/a&gt;) disable exceptions via &lt;code&gt;-fno-exceptions&lt;/code&gt; and rely on &lt;strong&gt;explicit error return values&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>RAII (Resource Acquisition Is Initialization)</title><link>/programming/cpp/raii/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/raii/</guid><description>&lt;h1 id="raii-resource-acquisition-is-initialization"&gt;RAII (Resource Acquisition Is Initialization)&lt;a class="anchor" href="#raii-resource-acquisition-is-initialization"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;A comprehensive guide to &lt;strong&gt;RAII (Resource Acquisition Is Initialization)&lt;/strong&gt;, the core resource management idiom in C++, covering its principles, exception safety via stack unwinding, standard library examples, and custom wrapper implementation.&lt;/p&gt;
&lt;h2 id="1-what-is-raii"&gt;1. What Is RAII?&lt;a class="anchor" href="#1-what-is-raii"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;RAII (Resource Acquisition Is Initialization)&lt;/strong&gt; is a foundational C++ design idiom that &lt;strong&gt;binds the lifecycle of any manually managed resource (heap memory, file descriptors, mutex locks, network sockets) to the lifetime of a stack-allocated local object&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Variables and Basic Types</title><link>/programming/cpp/primer/variables-and-basic-types/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/variables-and-basic-types/</guid><description>&lt;h1 id="variables-and-basic-types"&gt;Variables and Basic Types&lt;a class="anchor" href="#variables-and-basic-types"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 2 (Variables and Basic Types).&lt;/p&gt;
&lt;h2 id="type-definition"&gt;Type Definition&lt;a class="anchor" href="#type-definition"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The type of an object defines the &lt;strong&gt;data&lt;/strong&gt; that an object might contain and what &lt;strong&gt;operations&lt;/strong&gt; that object can perform. Among the operations that many types support is the ability to &lt;strong&gt;convert&lt;/strong&gt; objects of the given type to other, related types.&lt;/p&gt;
&lt;h2 id="string-literal"&gt;String Literal&lt;a class="anchor" href="#string-literal"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The type of a string literal is &lt;code&gt;const char *&lt;/code&gt;. See &lt;a href="../../programming/c/c-vs-cpp/#string-literal-typing"&gt;String Literal Typing&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Concurrency &amp; Threading</title><link>/programming/cpp/advanced/concurrency/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/advanced/concurrency/</guid><description>&lt;h1 id="c-concurrency-stdthread-vs-lightweight-fibers-channel--abseil-mutex"&gt;C++ Concurrency: &lt;code&gt;std::thread&lt;/code&gt; vs. Lightweight Fibers, &lt;code&gt;Channel&lt;/code&gt;, &amp;amp; Abseil &lt;code&gt;Mutex&lt;/code&gt;&lt;a class="anchor" href="#c-concurrency-stdthread-vs-lightweight-fibers-channel--abseil-mutex"&gt;#&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;This guide covers the architectural differences and usage patterns between &lt;strong&gt;OS kernel threads (&lt;code&gt;std::thread&lt;/code&gt; / &lt;code&gt;std::jthread&lt;/code&gt;)&lt;/strong&gt; and &lt;strong&gt;user-space lightweight threads (&lt;code&gt;thread::Fiber&lt;/code&gt;)&lt;/strong&gt;, &lt;strong&gt;CSP message passing (&lt;code&gt;thread::Channel&amp;lt;T&amp;gt;&lt;/code&gt; &amp;amp; &lt;code&gt;thread::Select&lt;/code&gt;) and how it directly inspired the Go programming language&lt;/strong&gt;, and synchronization with compile-time thread safety verification using open-source &lt;strong&gt;Abseil (&lt;code&gt;abseil-cpp&lt;/code&gt;)&lt;/strong&gt; primitives (&lt;code&gt;absl::Mutex&lt;/code&gt; and &lt;code&gt;ABSL_GUARDED_BY&lt;/code&gt;).&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-os-kernel-threads-stdthread-vs-user-space-lightweight-threads-fibers"&gt;1. OS Kernel Threads (&lt;code&gt;std::thread&lt;/code&gt;) vs. User-Space Lightweight Threads (Fibers)&lt;a class="anchor" href="#1-os-kernel-threads-stdthread-vs-user-space-lightweight-threads-fibers"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In modern high-performance servers, concurrency models generally fall into two categories: &lt;strong&gt;1:1 OS kernel threads&lt;/strong&gt; and &lt;strong&gt;M:N user-space lightweight threads (Fibers)&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Copy Elision &amp; Move Semantics</title><link>/programming/cpp/copy-elision-and-move-semantics/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/copy-elision-and-move-semantics/</guid><description>&lt;h1 id="copy-elision--move-semantics"&gt;Copy Elision &amp;amp; Move Semantics&lt;a class="anchor" href="#copy-elision--move-semantics"&gt;#&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;This guide traces the full story of &lt;strong&gt;Copy Elision&lt;/strong&gt; and &lt;strong&gt;Move Semantics&lt;/strong&gt; in Modern C++ (C++11+): why Copy Elision alone could not solve every copy overhead, how Move Semantics fills that exact gap, practical rules of thumb (&lt;strong&gt;single-live-name intuition&lt;/strong&gt;, parameter/return API guidelines, and &lt;code&gt;std::move&lt;/code&gt; best practices), language mechanics (&lt;code&gt;T&amp;amp;&lt;/code&gt; -&amp;gt; &lt;code&gt;const T&amp;amp;&lt;/code&gt; -&amp;gt; &lt;code&gt;T&amp;amp;&amp;amp;&lt;/code&gt;, &lt;code&gt;std::move&lt;/code&gt; vs. &lt;code&gt;std::forward&lt;/code&gt;), implementing &lt;code&gt;noexcept&lt;/code&gt; move operations, and ref-qualified member functions (&lt;code&gt;&amp;amp;&lt;/code&gt; / &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;).&lt;/p&gt;</description></item><item><title>Expressions</title><link>/programming/cpp/primer/expressions/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/expressions/</guid><description>&lt;h1 id="expressions"&gt;Expressions&lt;a class="anchor" href="#expressions"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 4 (Expressions).&lt;/p&gt;
&lt;h2 id="lvalues-and-rvalues"&gt;lvalues and rvalues&lt;a class="anchor" href="#lvalues-and-rvalues"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Every &lt;a href="../../programming/c/syntax-basics/#expression-vs-statement"&gt;expression&lt;/a&gt; in C++ is characterized by two independent properties: its &lt;strong&gt;type&lt;/strong&gt; and its &lt;strong&gt;value category&lt;/strong&gt;.&lt;/p&gt;
&lt;h3 id="core-distinction-expression-vs-variable-vs-object"&gt;Core Distinction: Expression vs. Variable vs. Object&lt;a class="anchor" href="#core-distinction-expression-vs-variable-vs-object"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Expression:&lt;/strong&gt; Possesses both a &lt;strong&gt;type&lt;/strong&gt; (reference stripped) and a &lt;strong&gt;value category&lt;/strong&gt; (lvalue, xvalue, prvalue). Value categories are strictly properties of &lt;em&gt;expressions&lt;/em&gt;, never of variables.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; Possesses only a &lt;strong&gt;declared type&lt;/strong&gt; (e.g., &lt;code&gt;int&lt;/code&gt;, &lt;code&gt;int&amp;amp;&lt;/code&gt;, &lt;code&gt;int&amp;amp;&amp;amp;&lt;/code&gt;); a variable itself has no value category. When a variable&amp;rsquo;s name appears in code as an expression, that &lt;em&gt;expression&lt;/em&gt; is evaluated as an lvalue (e.g., given &lt;code&gt;int &amp;amp;&amp;amp;rr = 10;&lt;/code&gt;, variable &lt;code&gt;rr&lt;/code&gt; has declared type &lt;code&gt;int&amp;amp;&amp;amp;&lt;/code&gt;, while the expression &lt;code&gt;rr&lt;/code&gt; has type &lt;code&gt;int&lt;/code&gt; and value category lvalue).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Object vs. Variable:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Object:&lt;/strong&gt; A region of storage in memory holding a value.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Variable:&lt;/strong&gt; A &lt;strong&gt;named object&lt;/strong&gt; or a &lt;strong&gt;named reference&lt;/strong&gt; introduced by a declaration.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Entity&lt;/th&gt;
 &lt;th style="text-align: center"&gt;Is Object? (Occupies Storage)&lt;/th&gt;
 &lt;th style="text-align: center"&gt;Is Variable? (Has Name)&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Note&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;x&lt;/code&gt; in &lt;code&gt;int x = 10;&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Named object in memory&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;r&lt;/code&gt; in &lt;code&gt;int &amp;amp;r = x;&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: center"&gt;❌&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Named reference (alias; unlike a pointer, &lt;code&gt;r&lt;/code&gt; has no separate address of its own distinct from &lt;code&gt;x&lt;/code&gt;, though used as an expression &lt;code&gt;r&lt;/code&gt; designates &lt;code&gt;x&lt;/code&gt; and is an lvalue with identity &lt;code&gt;&amp;amp;r == &amp;amp;x&lt;/code&gt;)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;10 + 20&lt;/code&gt; (temporary)&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: center"&gt;❌&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Unnamed temporary object&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="two-axes-of-value-categories"&gt;Two Axes of Value Categories&lt;a class="anchor" href="#two-axes-of-value-categories"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Identity:&lt;/strong&gt; Does the expression have an identifiable memory address (can we test whether two expressions designate the same entity with &lt;code&gt;&amp;amp;&lt;/code&gt;)?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Movable:&lt;/strong&gt; Can the object&amp;rsquo;s resources be safely stolen or moved because it is expiring?&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="value-categories-matrix-c11"&gt;Value Categories Matrix (C++11)&lt;a class="anchor" href="#value-categories-matrix-c11"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Non-movable&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Movable&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Has Identity&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;lvalue&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;xvalue&lt;/strong&gt; (&lt;em&gt;eXpiring&lt;/em&gt; value)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;No Identity&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;em&gt;(does not exist)&lt;/em&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;prvalue&lt;/strong&gt; (&lt;em&gt;pure&lt;/em&gt; rvalue)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt; Value Category
 / \
 glvalue rvalue
 / \ / \
 lvalue xvalue prvalue&lt;/code&gt;&lt;/pre&gt;&lt;h3 id="the-5-value-categories"&gt;The 5 Value Categories&lt;a class="anchor" href="#the-5-value-categories"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;h4 id="1-primary-categories"&gt;1. Primary Categories&lt;a class="anchor" href="#1-primary-categories"&gt;#&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;lvalue (Left Value):&lt;/strong&gt; Has identity and cannot be moved from. Represents persistent objects or functions in memory.
&lt;ul&gt;
&lt;li&gt;eg. Named variables (&lt;code&gt;x&lt;/code&gt;), dereference expressions (&lt;code&gt;*ptr&lt;/code&gt;), prefix increment/decrement (&lt;code&gt;++i&lt;/code&gt;), function calls returning lvalue references (&lt;code&gt;std::cout &amp;lt;&amp;lt; 1&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;prvalue (Pure Rvalue):&lt;/strong&gt; Has no identity and can be moved from. Represents transient values computed on the fly. In C++17, a prvalue is an evaluation recipe that initializes an object.
&lt;ul&gt;
&lt;li&gt;eg. Numeric/boolean literals (&lt;code&gt;42&lt;/code&gt;, &lt;code&gt;true&lt;/code&gt;, excluding string literals), arithmetic expressions (&lt;code&gt;a + b&lt;/code&gt;), function calls returning by value (&lt;code&gt;GetPoint()&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;xvalue (eXpiring Value):&lt;/strong&gt; Has identity and can be moved from. Designates an object nearing the end of its lifetime whose resources can be safely repurposed.
&lt;ul&gt;
&lt;li&gt;eg. Cast to rvalue reference (&lt;code&gt;std::move(x)&lt;/code&gt; / &lt;code&gt;static_cast&amp;lt;T&amp;amp;&amp;amp;&amp;gt;(x)&lt;/code&gt;), accessing a non-static member of an rvalue (&lt;code&gt;GetPoint().x&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4 id="2-composite-categories"&gt;2. Composite Categories&lt;a class="anchor" href="#2-composite-categories"&gt;#&lt;/a&gt;&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;glvalue (Generalized lvalue):&lt;/strong&gt; lvalue + xvalue. Any expression whose evaluation determines the identity of an object or function.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;rvalue:&lt;/strong&gt; prvalue + xvalue. Any expression eligible for move semantics.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="example"&gt;Example&lt;a class="anchor" href="#example"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;std&lt;span style="color:#0550ae"&gt;::&lt;/span&gt;string a &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0a3069"&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;std&lt;span style="color:#0550ae"&gt;::&lt;/span&gt;string b &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0a3069"&gt;&amp;#34;world&amp;#34;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;a&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// lvalue: has persistent identity, cannot be moved from
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;a &lt;span style="color:#0550ae"&gt;+&lt;/span&gt; b&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// prvalue: transient computed value without persistent identity
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;std&lt;span style="color:#0550ae"&gt;::&lt;/span&gt;move&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;a&lt;span style="color:#1f2328"&gt;);&lt;/span&gt; &lt;span style="color:#57606a"&gt;// xvalue: designates existing object &amp;#39;a&amp;#39;, but marked movable
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="decltype-two-step-deduction-rules-and-expressions"&gt;&lt;code&gt;decltype&lt;/code&gt; Two-Step Deduction Rules and Expressions&lt;a class="anchor" href="#decltype-two-step-deduction-rules-and-expressions"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;&lt;code&gt;decltype(...)&lt;/code&gt; evaluates its operand using a &lt;strong&gt;two-step rule (Rule 1 → Rule 2)&lt;/strong&gt;:&lt;/p&gt;</description></item><item><title>C vs C++</title><link>/programming/c/c-vs-cpp/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/c/c-vs-cpp/</guid><description>&lt;h1 id="c-vs-c"&gt;C vs C++&lt;a class="anchor" href="#c-vs-c"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="type-and-tag-namespaces"&gt;Type and Tag Namespaces&lt;a class="anchor" href="#type-and-tag-namespaces"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In C and C++, user-defined types are placed into different compiler namespaces, affecting whether type keywords can be omitted.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Type&lt;/th&gt;
 &lt;th style="text-align: left"&gt;C Syntax&lt;/th&gt;
 &lt;th style="text-align: left"&gt;C++ Syntax&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;struct&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;struct A a;&lt;/code&gt; (requires &lt;code&gt;typedef&lt;/code&gt; to omit)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;A a;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;union&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;union U u;&lt;/code&gt; (requires &lt;code&gt;typedef&lt;/code&gt; to omit)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;U u;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;enum&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;enum Color c;&lt;/code&gt; (requires &lt;code&gt;typedef&lt;/code&gt; to omit)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;Color c;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;class&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Not supported&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;MyClass obj;&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;h3 id="c-separate-tag-and-identifier-namespaces"&gt;C: Separate Tag and Identifier Namespaces&lt;a class="anchor" href="#c-separate-tag-and-identifier-namespaces"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In C, names of &lt;code&gt;struct&lt;/code&gt;, &lt;code&gt;union&lt;/code&gt;, and &lt;code&gt;enum&lt;/code&gt; reside in a dedicated &lt;strong&gt;tag namespace&lt;/strong&gt;, distinct from ordinary identifiers.&lt;/li&gt;
&lt;li&gt;Without a &lt;code&gt;typedef&lt;/code&gt;, variable declarations must include the defining keyword:
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;struct&lt;/span&gt; Node &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; val&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;struct&lt;/span&gt; Node n&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// &amp;#39;struct&amp;#39; keyword required in C
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="c-unified-type-namespace"&gt;C++: Unified Type Namespace&lt;a class="anchor" href="#c-unified-type-namespace"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;In C++, names introduced by &lt;code&gt;struct&lt;/code&gt;, &lt;code&gt;class&lt;/code&gt;, &lt;code&gt;union&lt;/code&gt;, or &lt;code&gt;enum&lt;/code&gt; automatically enter the &lt;strong&gt;type namespace&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The type name can be used directly without a &lt;code&gt;typedef&lt;/code&gt; or prefix keyword:
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;struct&lt;/span&gt; &lt;span style="color:#1f2328"&gt;Node&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; val&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;Node n&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Valid in C++ without typedef
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="string-literal-typing"&gt;String Literal Typing&lt;a class="anchor" href="#string-literal-typing"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Language&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Literal Type&lt;/th&gt;
 &lt;th style="text-align: left"&gt;&lt;code&gt;char *s = &amp;quot;hello&amp;quot;;&lt;/code&gt;&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Modification Attempt&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;C&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;char[]&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Valid (Legacy compatibility)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Runtime crash (Segmentation fault)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;C++&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;const char[]&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile Error&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Prevented at compile time&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;C:&lt;/strong&gt; String literals are stored in read-only memory, but their formal type is non-const &lt;code&gt;char[]&lt;/code&gt;. Modifying a literal triggers undefined behavior at runtime.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;C++:&lt;/strong&gt; String literals have type &lt;code&gt;const char[]&lt;/code&gt; (decaying to &lt;code&gt;const char *&lt;/code&gt;). The compiler prevents modifications and invalid assignments at compile time (enforcing low-level const).&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="references-vs-pointers"&gt;References vs. Pointers&lt;a class="anchor" href="#references-vs-pointers"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C++ introduces references (&lt;code&gt;&amp;amp;&lt;/code&gt;), which do not exist in C.&lt;/p&gt;</description></item><item><title>Most Vexing Parse &amp; In-Class Initializer</title><link>/programming/cpp/most-vexing-parse/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/most-vexing-parse/</guid><description>&lt;h1 id="most-vexing-parse--in-class-initializer"&gt;Most Vexing Parse &amp;amp; In-Class Initializer&lt;a class="anchor" href="#most-vexing-parse--in-class-initializer"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;This page covers the exact conditions that trigger the &lt;strong&gt;Most Vexing Parse&lt;/strong&gt; when initializing objects with &lt;code&gt;()&lt;/code&gt; in C++, contrasts it with the rule that &lt;strong&gt;prohibits &lt;code&gt;()&lt;/code&gt; altogether for in-class member initializers&lt;/strong&gt;, and explains the &lt;strong&gt;&lt;code&gt;std::initializer_list&lt;/code&gt; constructor hijacking pitfall&lt;/strong&gt; when using braces &lt;code&gt;{}&lt;/code&gt; as a workaround.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-what-is-the-most-vexing-parse"&gt;1. What Is the Most Vexing Parse?&lt;a class="anchor" href="#1-what-is-the-most-vexing-parse"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Coined by Scott Meyers in &lt;em&gt;Effective STL&lt;/em&gt; (Item 6), the &lt;strong&gt;Most Vexing Parse&lt;/strong&gt; refers to a counterintuitive consequence of C++&amp;rsquo;s syntactic disambiguation rule where &lt;strong&gt;code intended to instantiate an object using &lt;code&gt;()&lt;/code&gt; is instead parsed by the compiler as a function declaration&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Type Conversions and Casts</title><link>/programming/cpp/primer/type-conversions-and-casts/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/type-conversions-and-casts/</guid><description>&lt;h1 id="type-conversions-and-casts"&gt;Type Conversions and Casts&lt;a class="anchor" href="#type-conversions-and-casts"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Section 4.11 (Type Conversions).&lt;/p&gt;
&lt;h2 id="named-cast-syntax"&gt;Named Cast Syntax&lt;a class="anchor" href="#named-cast-syntax"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C++ introduces four explicit named casts:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;CASTNAME&lt;span style="color:#0550ae"&gt;&amp;lt;&lt;/span&gt;TYPE&lt;span style="color:#0550ae"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;EXPRESSION&lt;span style="color:#1f2328"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;CASTNAME&lt;/code&gt;:&lt;/strong&gt; One of &lt;code&gt;static_cast&lt;/code&gt;, &lt;code&gt;const_cast&lt;/code&gt;, &lt;code&gt;reinterpret_cast&lt;/code&gt;, or &lt;code&gt;dynamic_cast&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Value Category Rule:&lt;/strong&gt; If &lt;code&gt;TYPE&lt;/code&gt; is a reference, the result of the cast is an &lt;strong&gt;lvalue&lt;/strong&gt;; otherwise, the result is an &lt;strong&gt;rvalue&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="the-four-named-casts-at-a-glance"&gt;The Four Named Casts at a Glance&lt;a class="anchor" href="#the-four-named-casts-at-a-glance"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Cast&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Timing&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Target / Purpose&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Safety Check&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;static_cast&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile-time&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Well-defined conversions (arithmetic, &lt;code&gt;void*&lt;/code&gt; recovery, upcasting)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Validates convertible types at compile time&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;const_cast&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile-time&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Adds or casts away low-level &lt;code&gt;const&lt;/code&gt; / &lt;code&gt;volatile&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compiler ensures types match except for constness&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;reinterpret_cast&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Compile-time&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Low-level bit pattern reinterpretation&lt;/td&gt;
 &lt;td style="text-align: left"&gt;No safety check (platform-dependent, aliasing risks)&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;dynamic_cast&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Runtime&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Safe Downcasting &amp;amp; Crosscasting in polymorphic hierarchies&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Queries RTTI (Run-Time Type Identification) via &lt;code&gt;vptr&lt;/code&gt; → &lt;code&gt;vtable&lt;/code&gt;&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="1-static_cast"&gt;1. &lt;code&gt;static_cast&lt;/code&gt;&lt;a class="anchor" href="#1-static_cast"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Used for any well-defined type conversion that does not involve casting away &lt;code&gt;const&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>C++ Keywords Reference</title><link>/programming/cpp/keywords/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/keywords/</guid><description>&lt;h1 id="c-keywords-reference"&gt;C++ Keywords Reference&lt;a class="anchor" href="#c-keywords-reference"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;This post consolidates core C++ and Modern C++ (C++11+) keywords, organized by role.&lt;/p&gt;
&lt;h3 id="anatomy-of-keyword-placement-in-a-function-declaration"&gt;Anatomy of Keyword Placement in a Function Declaration&lt;a class="anchor" href="#anatomy-of-keyword-placement-in-a-function-declaration"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Keywords attached to a C++ function declaration are placed into &lt;strong&gt;Declaration Specifiers (prefix) / Function Qualifiers &amp;amp; Specifiers (after &lt;code&gt;()&lt;/code&gt;) / Function Body (at the end)&lt;/strong&gt; according to the C++ grammar and compiler parsing order:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;virtual const int&amp;amp; FUNCTION(PARAM_TYPE PARAM_NAME) const &amp;amp; noexcept = 0;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;─────── ────────── ─────────────────────────────── ──────────────── ───
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; │ │ │ │ └─ Function Body: { ... }, = default, = delete, = 0
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; │ │ │ └─ Function Qualifiers &amp;amp; Specifiers: const/volatile, &amp;amp;/&amp;amp;&amp;amp;, noexcept, override, final
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; │ │ └─ Function Name(Params)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; │ └─ Return Type (prefix const = const return value)
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; └─ Declaration Specifiers: explicit, virtual, inline, static, constexpr&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;1. Declaration Specifiers (before function: &lt;code&gt;explicit&lt;/code&gt;, &lt;code&gt;virtual&lt;/code&gt;, &lt;code&gt;inline&lt;/code&gt;, &lt;code&gt;static&lt;/code&gt;, &lt;code&gt;constexpr&lt;/code&gt;):&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;Declare &lt;strong&gt;how the function is linked, inlined, or invoked&lt;/strong&gt; before reaching the function name, independent of its parameters or body.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;2. Function Qualifiers &amp;amp; Specifiers (after &lt;code&gt;()&lt;/code&gt;: &lt;code&gt;const&lt;/code&gt;/&lt;code&gt;volatile&lt;/code&gt;, &lt;code&gt;&amp;amp;&lt;/code&gt;/&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;, &lt;code&gt;noexcept&lt;/code&gt;, &lt;code&gt;override&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt;):&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Avoiding Return-Type Ambiguity (&lt;code&gt;const&lt;/code&gt;, &lt;code&gt;&amp;amp;&lt;/code&gt; / &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;):&lt;/strong&gt; Writing &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;&amp;amp;&lt;/code&gt;/&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; in front (&lt;code&gt;const int&amp;amp; func()&lt;/code&gt;) qualifies the &lt;strong&gt;return type&lt;/strong&gt;, not the member function. Placing them after &lt;code&gt;()&lt;/code&gt; unambiguously qualifies the implicit &lt;code&gt;*this&lt;/code&gt; object&amp;rsquo;s constness (&lt;code&gt;const&lt;/code&gt;) and value category (&lt;code&gt;&amp;amp;&lt;/code&gt; = lvalue, &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; = rvalue) &lt;em&gt;(see &lt;a href="../../programming/cpp/copy-elision-and-move-semantics/#6-ref-qualified-member-functions"&gt;Copy Elision &amp;amp; Move Semantics: Ref-qualified Member Functions&lt;/a&gt;)&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Referencing Parameter Scope (&lt;code&gt;noexcept&lt;/code&gt;):&lt;/strong&gt; The compiler parses left-to-right, and conditional exception specifications &lt;code&gt;noexcept(expr)&lt;/code&gt; &lt;strong&gt;must be able to reference parameter names&lt;/strong&gt; (e.g., &lt;code&gt;void f(T x) noexcept(x.is_safe())&lt;/code&gt;). Thus, &lt;code&gt;noexcept&lt;/code&gt; must appear after the parameter list &lt;code&gt;( ... )&lt;/code&gt; has been declared.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;3. Function Body &amp;amp; Pure Specifier (at the end after &lt;code&gt;=&lt;/code&gt;: &lt;code&gt;= default&lt;/code&gt;, &lt;code&gt;= delete&lt;/code&gt;, &lt;code&gt;= 0&lt;/code&gt;):&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Replacing &lt;code&gt;{ ... }&lt;/code&gt;:&lt;/strong&gt; These stand directly in place of the function body &lt;code&gt;{ ... }&lt;/code&gt; at the very end of the declaration.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why &lt;code&gt;=&lt;/code&gt; is used:&lt;/strong&gt; Following the classic pure virtual syntax (&lt;code&gt;= 0&lt;/code&gt;), C++11 &lt;strong&gt;reused existing keywords (&lt;code&gt;default&lt;/code&gt; from &lt;code&gt;switch&lt;/code&gt; and &lt;code&gt;delete&lt;/code&gt; from memory deallocation) after &lt;code&gt;=&lt;/code&gt;&lt;/strong&gt; to avoid introducing new reserved words while clearly signaling a special function-body definition.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="1-special-member-function--conversion-control-keywords--default--delete-explicit-noexcept"&gt;1. Special Member Function &amp;amp; Conversion Control Keywords (&lt;code&gt;= default&lt;/code&gt;, &lt;code&gt;= delete&lt;/code&gt;, &lt;code&gt;explicit&lt;/code&gt;, &lt;code&gt;noexcept&lt;/code&gt;)&lt;a class="anchor" href="#1-special-member-function--conversion-control-keywords--default--delete-explicit-noexcept"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;These keywords control constructors, destructors, copy/move operations, and type conversions at compile time (see &lt;a href="../../programming/cpp/primer/copy-control/"&gt;Copy Control&lt;/a&gt; and &lt;a href="../../programming/cpp/copy-elision-and-move-semantics/"&gt;Copy Elision &amp;amp; Move Semantics&lt;/a&gt;).&lt;/p&gt;</description></item><item><title>Statements</title><link>/programming/cpp/primer/statements/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/statements/</guid><description>&lt;h1 id="statements"&gt;Statements&lt;a class="anchor" href="#statements"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Key statement syntax and critical caveats based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 5 (Statements).&lt;/p&gt;
&lt;h2 id="1-the-if-statement-and-dangling-else"&gt;1. The &lt;code&gt;if&lt;/code&gt; Statement and Dangling Else&lt;a class="anchor" href="#1-the-if-statement-and-dangling-else"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In C++, an &lt;code&gt;else&lt;/code&gt; branch is syntactically paired with the &lt;strong&gt;closest preceding unmatched &lt;code&gt;if&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;if&lt;/span&gt; &lt;span style="color:#1f2328"&gt;(&lt;/span&gt;condition1&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;if&lt;/span&gt; &lt;span style="color:#1f2328"&gt;(&lt;/span&gt;condition2&lt;span style="color:#1f2328"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; DoSomething&lt;span style="color:#1f2328"&gt;();&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;else&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Warning: pairs with condition2&amp;#39;s if, not condition1!
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; DoAnother&lt;span style="color:#1f2328"&gt;();&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;To eliminate ambiguity and prevent logical bugs, always use explicit curly braces (&lt;code&gt;{}&lt;/code&gt;) to wrap compound statement blocks.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="2-the-switch-statement-and-bypassing-variable-initialization"&gt;2. The &lt;code&gt;switch&lt;/code&gt; Statement and Bypassing Variable Initialization&lt;a class="anchor" href="#2-the-switch-statement-and-bypassing-variable-initialization"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The curly braces &lt;code&gt;{ ... }&lt;/code&gt; wrapping the body of a &lt;code&gt;switch&lt;/code&gt; statement form a single scope. A &lt;code&gt;case&lt;/code&gt; label is merely a jump target and does not establish a separate nested scope.&lt;/p&gt;</description></item><item><title>Unit Testing</title><link>/programming/cpp/advanced/unit-testing/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/advanced/unit-testing/</guid><description>&lt;h1 id="unit-testing"&gt;Unit Testing&lt;a class="anchor" href="#unit-testing"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Practical guidelines for C++ unit testing with open-source &lt;a href="https://github.com/google/googletest"&gt;GoogleTest (&lt;code&gt;google/googletest&lt;/code&gt;)&lt;/a&gt; and &lt;strong&gt;Abseil (&lt;code&gt;abseil-cpp&lt;/code&gt;)&lt;/strong&gt;, covering test file namespace layout, how &lt;code&gt;TEST&lt;/code&gt; and &lt;code&gt;TEST_F&lt;/code&gt; expand under the hood, &lt;code&gt;Test Fixture&lt;/code&gt; lifecycles, and choosing between &lt;code&gt;CHECK&lt;/code&gt;, &lt;code&gt;ASSERT_*&lt;/code&gt;, and &lt;code&gt;EXPECT_*&lt;/code&gt;.&lt;/p&gt;
&lt;hr&gt;
&lt;h2 id="1-namespace-structure-in-test-files"&gt;1. Namespace Structure in Test Files&lt;a class="anchor" href="#1-namespace-structure-in-test-files"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="1-idiomatic-layout-namespace-pkg--namespace----"&gt;1. Idiomatic Layout: &lt;code&gt;namespace &amp;lt;pkg&amp;gt; { namespace { ... } }&lt;/code&gt;&lt;a class="anchor" href="#1-idiomatic-layout-namespace-pkg--namespace----"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;When &lt;code&gt;greeter.h&lt;/code&gt; and &lt;code&gt;greeter.cc&lt;/code&gt; declare symbols inside &lt;code&gt;namespace greeter&lt;/code&gt;, the corresponding &lt;code&gt;greeter_test.cc&lt;/code&gt; should open the &lt;strong&gt;same outer &lt;code&gt;namespace greeter&lt;/code&gt;&lt;/strong&gt; and wrap the test definitions inside an inner &lt;strong&gt;&lt;a href="../../programming/cpp/primer/namespaces/"&gt;anonymous namespace&lt;/a&gt; (&lt;code&gt;namespace { ... }&lt;/code&gt;)&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Functions</title><link>/programming/cpp/primer/functions/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/functions/</guid><description>&lt;h1 id="functions"&gt;Functions&lt;a class="anchor" href="#functions"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 6 (Functions).&lt;/p&gt;
&lt;p&gt;A function is a block of code with a name. Functions can be overloaded, meaning that the same name may refer to several different functions.&lt;/p&gt;
&lt;h2 id="1-object-lifetime-and-function-declarations"&gt;1. Object Lifetime and Function Declarations&lt;a class="anchor" href="#1-object-lifetime-and-function-declarations"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="automatic-objects"&gt;Automatic Objects&lt;a class="anchor" href="#automatic-objects"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;An automatic object is an object that exists only while a block is executing.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Parameters are automatic objects.&lt;/strong&gt; They are created (initialized) when the function is called and destroyed when the block is exited.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="local-static-objects"&gt;Local &lt;code&gt;static&lt;/code&gt; Objects&lt;a class="anchor" href="#local-static-objects"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Each local &lt;code&gt;static&lt;/code&gt; object is initialized before the first time execution passes through the object&amp;rsquo;s definition.&lt;/p&gt;</description></item><item><title>Classes</title><link>/programming/cpp/primer/classes/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/classes/</guid><description>&lt;h1 id="classes"&gt;Classes&lt;a class="anchor" href="#classes"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 7 (Classes).&lt;/p&gt;
&lt;h2 id="1-member-functions-and-the-implicit-this-pointer"&gt;1. Member Functions and the Implicit &lt;code&gt;this&lt;/code&gt; Pointer&lt;a class="anchor" href="#1-member-functions-and-the-implicit-this-pointer"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Member functions access the object on which they were called through an extra, implicit parameter named &lt;code&gt;this&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It is illegal for us to define a parameter or variable named &lt;code&gt;this&lt;/code&gt;, because &lt;code&gt;this&lt;/code&gt; is intended to always refer to &amp;ldquo;this&amp;rdquo; object, &lt;code&gt;this&lt;/code&gt; is a &lt;code&gt;const&lt;/code&gt; pointer. (&lt;code&gt;CLASS *const this&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="const-member-functions"&gt;&lt;code&gt;const&lt;/code&gt; Member Functions&lt;a class="anchor" href="#const-member-functions"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;A &lt;strong&gt;&lt;code&gt;const&lt;/code&gt; member function&lt;/strong&gt; (a member function with &lt;code&gt;const&lt;/code&gt; placed after the parameter list &lt;code&gt;()&lt;/code&gt;, e.g., &lt;code&gt;void bar() const&lt;/code&gt;) changes &lt;code&gt;this&lt;/code&gt; to &lt;code&gt;const CLASS *const this&lt;/code&gt; so that the object cannot be modified.&lt;/p&gt;</description></item><item><title>The IO Library</title><link>/programming/cpp/primer/io-library/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/io-library/</guid><description>&lt;h1 id="the-io-library"&gt;The IO Library&lt;a class="anchor" href="#the-io-library"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Summary based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 8 (The IO Library).&lt;/p&gt;
&lt;h2 id="1-io-library-headers-and-inheritance-hierarchy"&gt;1. IO Library Headers and Inheritance Hierarchy&lt;a class="anchor" href="#1-io-library-headers-and-inheritance-hierarchy"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The C++ IO library is organized into three main headers handling console, file, and in-memory string streams, sharing a unified interface through &lt;strong&gt;inheritance&lt;/strong&gt;.&lt;/p&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Header&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Core Types / Objects&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Base Class (&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;, &lt;code&gt;getline&lt;/code&gt; Inherited)&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Type-Specific Member Functions&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Role&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;iostream&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Types:&lt;/strong&gt; &lt;code&gt;istream&lt;/code&gt;, &lt;code&gt;ostream&lt;/code&gt;, &lt;code&gt;iostream&lt;/code&gt;&lt;br&gt;&lt;strong&gt;Global Objects:&lt;/strong&gt; &lt;code&gt;cin&lt;/code&gt;, &lt;code&gt;cout&lt;/code&gt;, &lt;code&gt;cerr&lt;/code&gt; (unbuffered), &lt;code&gt;clog&lt;/code&gt; (buffered)&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Root stream base classes&lt;/td&gt;
 &lt;td style="text-align: left"&gt;-&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Console (standard I/O) streams&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;fstream&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;ifstream&lt;/code&gt;, &lt;code&gt;ofstream&lt;/code&gt;, &lt;code&gt;fstream&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Inherits from &lt;code&gt;istream&lt;/code&gt;, &lt;code&gt;ostream&lt;/code&gt;, &lt;code&gt;iostream&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.open()&lt;/code&gt;, &lt;code&gt;.close()&lt;/code&gt;, &lt;code&gt;.is_open()&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;File I/O streams&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;sstream&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;istringstream&lt;/code&gt;, &lt;code&gt;ostringstream&lt;/code&gt;, &lt;code&gt;stringstream&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Inherits from &lt;code&gt;istream&lt;/code&gt;, &lt;code&gt;ostream&lt;/code&gt;, &lt;code&gt;iostream&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;.str()&lt;/code&gt;, &lt;code&gt;.str(S)&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;In-memory &lt;code&gt;std::string&lt;/code&gt; streams&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;blockquote class='book-hint '&gt;
&lt;p&gt;&lt;strong&gt;Benefit of Polymorphism:&lt;/strong&gt; Because &lt;code&gt;ifstream&lt;/code&gt; and &lt;code&gt;istringstream&lt;/code&gt; both inherit from &lt;code&gt;std::istream&lt;/code&gt;, any function taking a &lt;code&gt;std::istream&amp;amp;&lt;/code&gt; reference (e.g., &lt;code&gt;read(std::istream&amp;amp;, Sales_data&amp;amp;)&lt;/code&gt;) works seamlessly with &lt;code&gt;std::cin&lt;/code&gt;, file streams (&lt;code&gt;ifstream&lt;/code&gt;), and string streams (&lt;code&gt;istringstream&lt;/code&gt;) without modification.&lt;/p&gt;</description></item><item><title>STL</title><link>/programming/cpp/primer/stl/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/stl/</guid><description>&lt;h1 id="stl"&gt;STL&lt;a class="anchor" href="#stl"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Summary of core iterator traversal and erase patterns and element insertion (&lt;code&gt;push_back&lt;/code&gt; vs &lt;code&gt;emplace_back&lt;/code&gt;) from &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 9 (Sequential Containers).&lt;/p&gt;
&lt;h2 id="1-iterator-traversal--erase-patterns"&gt;1. Iterator Traversal &amp;amp; Erase Patterns&lt;a class="anchor" href="#1-iterator-traversal--erase-patterns"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;h3 id="1-forward-traversal"&gt;1. Forward Traversal&lt;a class="anchor" href="#1-forward-traversal"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;When elements do not need to be modified, prefer constant iterators (&lt;code&gt;cbegin()&lt;/code&gt; / &lt;code&gt;cend()&lt;/code&gt;).&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;for&lt;/span&gt; &lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;auto&lt;/span&gt; it &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; v&lt;span style="color:#1f2328"&gt;.&lt;/span&gt;cbegin&lt;span style="color:#1f2328"&gt;();&lt;/span&gt; it &lt;span style="color:#0550ae"&gt;!=&lt;/span&gt; v&lt;span style="color:#1f2328"&gt;.&lt;/span&gt;cend&lt;span style="color:#1f2328"&gt;();&lt;/span&gt; &lt;span style="color:#0550ae"&gt;++&lt;/span&gt;it&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#57606a"&gt;/* process *it */&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="2-reverse-traversal-with-bidirectional-iterators"&gt;2. Reverse Traversal with Bidirectional Iterators&lt;a class="anchor" href="#2-reverse-traversal-with-bidirectional-iterators"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Because &lt;code&gt;cend()&lt;/code&gt; points &lt;strong&gt;one past the last element&lt;/strong&gt; rather than to the last element itself, decrement &lt;code&gt;--it&lt;/code&gt; at the very beginning of the loop body right after the condition check.&lt;/p&gt;</description></item><item><title>Lambda Expressions</title><link>/programming/cpp/primer/lambda/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/lambda/</guid><description>&lt;h1 id="lambda-expressions"&gt;Lambda Expressions&lt;a class="anchor" href="#lambda-expressions"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Summary based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 10.3 and Chapter 14.8.1.&lt;/p&gt;
&lt;h2 id="1-syntax-and-under-the-hood-mechanism"&gt;1. Syntax and Under-the-Hood Mechanism&lt;a class="anchor" href="#1-syntax-and-under-the-hood-mechanism"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;[&lt;/span&gt;CAPTURE_LIST&lt;span style="color:#1f2328"&gt;](&lt;/span&gt;PARAMETER_LIST&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#cf222e"&gt;mutable&lt;/span&gt; &lt;span style="color:#0550ae"&gt;-&amp;gt;&lt;/span&gt; RETURN_TYPE &lt;span style="color:#1f2328"&gt;{&lt;/span&gt; &lt;span style="color:#57606a"&gt;/* FUNCTION_BODY */&lt;/span&gt; &lt;span style="color:#1f2328"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ul&gt;
&lt;li&gt;While &lt;code&gt;PARAMETER_LIST&lt;/code&gt;, &lt;code&gt;mutable&lt;/code&gt;, and &lt;code&gt;-&amp;gt; RETURN_TYPE&lt;/code&gt; are optional, &lt;strong&gt;the capture list &lt;code&gt;[]&lt;/code&gt; and function body &lt;code&gt;{}&lt;/code&gt; are always required&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Under-the-Hood Mechanism:&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;A lambda expression is syntactic sugar for &lt;strong&gt;an unnamed Closure class and a temporary Closure object&lt;/strong&gt; generated automatically by the compiler:
&lt;ol&gt;
&lt;li&gt;Items in the &lt;strong&gt;capture list &lt;code&gt;[CAPTURE_LIST]&lt;/code&gt;&lt;/strong&gt; become &lt;strong&gt;data members&lt;/strong&gt; of the unnamed class, initialized via the class&amp;rsquo;s &lt;strong&gt;constructor initializer list&lt;/strong&gt; when the lambda is created.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;parameter list &lt;code&gt;(PARAMETER_LIST)&lt;/code&gt; and body &lt;code&gt;{ /* FUNCTION_BODY */ }&lt;/code&gt;&lt;/strong&gt; become the class&amp;rsquo;s overloaded &lt;strong&gt;function-call operator &lt;code&gt;operator()&lt;/code&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Crucially, &lt;code&gt;operator()&lt;/code&gt; is generated as a &lt;strong&gt;&lt;code&gt;const&lt;/code&gt; member function &lt;code&gt;operator()(PARAMETER_LIST) const&lt;/code&gt; by default&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;// Developer&amp;#39;s lambda code
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;size_t v1 &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0550ae"&gt;42&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;auto&lt;/span&gt; f &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#1f2328"&gt;[&lt;/span&gt;v1&lt;span style="color:#1f2328"&gt;](&lt;/span&gt;size_t x&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt; &lt;span style="color:#cf222e"&gt;return&lt;/span&gt; v1 &lt;span style="color:#0550ae"&gt;+&lt;/span&gt; x&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#1f2328"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;// Closure class generated by the compiler
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;class&lt;/span&gt; &lt;span style="color:#1f2328"&gt;__Lambda&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;private&lt;/span&gt;&lt;span style="color:#0550ae"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; size_t v1&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// 1. Captured items become data members of the closure class
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;public&lt;/span&gt;&lt;span style="color:#0550ae"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; __Lambda&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;size_t v&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#0550ae"&gt;:&lt;/span&gt; v1&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;v&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{}&lt;/span&gt; &lt;span style="color:#57606a"&gt;// 2. Initialized at lambda creation time via initializer list
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#57606a"&gt;// 3. The lambda body becomes a const member function operator() by default
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;auto&lt;/span&gt; &lt;span style="color:#6639ba"&gt;operator&lt;/span&gt;&lt;span style="color:#1f2328"&gt;()(&lt;/span&gt;size_t x&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#cf222e"&gt;const&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;return&lt;/span&gt; v1 &lt;span style="color:#0550ae"&gt;+&lt;/span&gt; x&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#1f2328"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;auto&lt;/span&gt; f &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; __Lambda&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;v1&lt;span style="color:#1f2328"&gt;);&lt;/span&gt; &lt;span style="color:#57606a"&gt;// &amp;#39;f&amp;#39; is an instance of this compiler-generated class
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id="2-the-core-unified-principle-two-steps-govern-every-capture--const-case"&gt;2. The Core Unified Principle: Two Steps Govern Every Capture &amp;amp; &lt;code&gt;const&lt;/code&gt; Case&lt;a class="anchor" href="#2-the-core-unified-principle-two-steps-govern-every-capture--const-case"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Every capture rule in C++ follows a single &lt;strong&gt;2-step principle&lt;/strong&gt;:&lt;/p&gt;</description></item><item><title>Smart Pointers</title><link>/programming/cpp/primer/smart-pointers/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/smart-pointers/</guid><description>&lt;h1 id="smart-pointers"&gt;Smart Pointers&lt;a class="anchor" href="#smart-pointers"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Summary based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 12 (Dynamic Memory and Smart Pointers).&lt;/p&gt;
&lt;h2 id="1-why-smart-pointers-exist-raii-and-ownership"&gt;1. Why Smart Pointers Exist (RAII and Ownership)&lt;a class="anchor" href="#1-why-smart-pointers-exist-raii-and-ownership"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Stack vs. Heap Lifetimes:&lt;/strong&gt; Stack local variables automatically invoke their destructor (&lt;code&gt;~Class()&lt;/code&gt;) when leaving scope (&lt;code&gt;{ ... }&lt;/code&gt;), whereas heap memory allocated via &lt;code&gt;new&lt;/code&gt; is NOT automatically freed when a raw pointer (&lt;code&gt;T*&lt;/code&gt;) goes out of scope, leading to memory leaks.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="../../programming/cpp/raii/"&gt;RAII (Resource Acquisition Is Initialization)&lt;/a&gt;:&lt;/strong&gt; By wrapping a raw pointer (&lt;code&gt;T*&lt;/code&gt;) inside a &lt;strong&gt;stack-allocated wrapper class&lt;/strong&gt; whose &lt;strong&gt;destructor (&lt;code&gt;~SmartPtr()&lt;/code&gt;) automatically calls &lt;code&gt;delete ptr;&lt;/code&gt;&lt;/strong&gt;, heap memory is deterministically freed when the wrapper leaves scope.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The Copy / Double-Free Problem and Ownership Models:&lt;/strong&gt; If two smart pointers pointed to the same heap address and both called &lt;code&gt;delete&lt;/code&gt; in their destructors, a fatal &lt;strong&gt;Double Free&lt;/strong&gt; crash would occur. To solve this, C++11 &lt;code&gt;&amp;lt;memory&amp;gt;&lt;/code&gt; provides three smart pointers with distinct ownership semantics:&lt;/li&gt;
&lt;/ul&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Smart Pointer&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Ownership Model&lt;/th&gt;
 &lt;th style="text-align: center"&gt;Copyable?&lt;/th&gt;
 &lt;th style="text-align: center"&gt;&lt;code&gt;*ptr&lt;/code&gt; / &lt;code&gt;ptr-&amp;gt;&lt;/code&gt; Dereference&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Key Characteristics &amp;amp; Use Case&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;std::unique_ptr&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Exclusive Ownership&lt;/strong&gt; — 1:1&lt;/td&gt;
 &lt;td style="text-align: center"&gt;❌ &lt;code&gt;std::move&lt;/code&gt; only&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Zero overhead with the same size and speed as &lt;code&gt;T*&lt;/code&gt;; the default choice for dynamic memory.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;std::shared_ptr&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Shared Ownership&lt;/strong&gt; — N:1&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅ updates &lt;code&gt;use_count&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Calls &lt;code&gt;delete&lt;/code&gt; only when the strong reference count &lt;code&gt;use_count&lt;/code&gt; in the Control Block reaches 0.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;std::weak_ptr&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Non-Owning&lt;/strong&gt; — Observer&lt;/td&gt;
 &lt;td style="text-align: center"&gt;✅ &lt;code&gt;use_count&lt;/code&gt; unchanged&lt;/td&gt;
 &lt;td style="text-align: center"&gt;❌ &lt;code&gt;.lock()&lt;/code&gt; required&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Observes a &lt;code&gt;shared_ptr&lt;/code&gt;-managed object without incrementing &lt;code&gt;use_count&lt;/code&gt;, breaking &lt;strong&gt;circular reference memory leaks&lt;/strong&gt;.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="2-stdunique_ptr-exclusive-ownership-and-stdmake_unique"&gt;2. &lt;code&gt;std::unique_ptr&lt;/code&gt;: Exclusive Ownership and &lt;code&gt;std::make_unique&lt;/code&gt;&lt;a class="anchor" href="#2-stdunique_ptr-exclusive-ownership-and-stdmake_unique"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Because only one &lt;code&gt;unique_ptr&lt;/code&gt; can own a given object at a time, its copy constructor and copy-assignment operator are deleted (&lt;code&gt;= delete&lt;/code&gt;).&lt;/p&gt;</description></item><item><title>Copy Control</title><link>/programming/cpp/primer/copy-control/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/copy-control/</guid><description>&lt;h1 id="copy-control"&gt;Copy Control&lt;a class="anchor" href="#copy-control"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Summary based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Chapter 13: Copy Control, covering the five special member functions that govern object initialization, assignment, movement, and destruction, along with essential control keywords. For a dedicated deep dive into Move Semantics, &lt;code&gt;T&amp;amp;&amp;amp;&lt;/code&gt;, &lt;code&gt;std::move&lt;/code&gt;, and move constructor/assignment implementation, see &lt;a href="../../programming/cpp/copy-elision-and-move-semantics/"&gt;Copy Elision &amp;amp; Move Semantics&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="1-the-five-copy-control-special-member-functions"&gt;1. The Five Copy-Control Special Member Functions&lt;a class="anchor" href="#1-the-five-copy-control-special-member-functions"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In C++, &lt;strong&gt;five special member functions&lt;/strong&gt; govern an object&amp;rsquo;s lifecycle and resource copying/moving across three lifecycle stages:&lt;/p&gt;</description></item><item><title>Overloaded Operations and Conversions</title><link>/programming/cpp/primer/operator-overloading/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/operator-overloading/</guid><description>&lt;h1 id="overloaded-operations-and-conversions"&gt;Overloaded Operations and Conversions&lt;a class="anchor" href="#overloaded-operations-and-conversions"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt;, Chapter 14: Overloaded Operations and Conversions, this post summarizes design principles for operator overloading, implementation idioms for major operators, &lt;strong&gt;Functors&lt;/strong&gt; &lt;code&gt;operator()&lt;/code&gt;, &lt;code&gt;std::function&lt;/code&gt;, and conversion operators &lt;code&gt;operator TYPE()&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id="1-operator-overloading-fundamentals--member-vs-nonmember-guidelines"&gt;1. Operator Overloading Fundamentals &amp;amp; Member vs. Nonmember Guidelines&lt;a class="anchor" href="#1-operator-overloading-fundamentals--member-vs-nonmember-guidelines"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Overloaded operators are functions named with the &lt;code&gt;operator&lt;/code&gt; keyword followed by the operator symbol (&lt;code&gt;operator+&lt;/code&gt;, &lt;code&gt;operator=&lt;/code&gt;, &lt;code&gt;operator()&lt;/code&gt;, etc.).&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Four Operators That Cannot Be Overloaded:&lt;/strong&gt; Scope resolution &lt;code&gt;::&lt;/code&gt;, member selection &lt;code&gt;.&lt;/code&gt;, &lt;a href="../../programming/cpp/primer/pointers-to-class-member/"&gt;pointer-to-member dereference &lt;code&gt;.*&lt;/code&gt;&lt;/a&gt; — whereas &lt;code&gt;*&lt;/code&gt; and &lt;code&gt;-&amp;gt;*&lt;/code&gt; &lt;em&gt;can&lt;/em&gt; be overloaded, and the ternary conditional &lt;code&gt;?:&lt;/code&gt; operator cannot be overloaded.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Operators You Should Not Overload:&lt;/strong&gt; Overloading logical AND (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;), logical OR (&lt;code&gt;||&lt;/code&gt;), or the comma operator (&lt;code&gt;,&lt;/code&gt;) loses built-in &lt;strong&gt;short-circuit evaluation&lt;/strong&gt; and &lt;strong&gt;order-of-evaluation guarantees&lt;/strong&gt;, so they should not be overloaded. The unary address-of operator (&lt;code&gt;&amp;amp;&lt;/code&gt;) should also not be overloaded for class types.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="choosing-member-this-vs-nonmember-friend-implementation"&gt;Choosing Member (&lt;code&gt;this&lt;/code&gt;) vs. Nonmember (&lt;code&gt;friend&lt;/code&gt;) Implementation&lt;a class="anchor" href="#choosing-member-this-vs-nonmember-friend-implementation"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Category&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Target Operators&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Rationale &amp;amp; Core Rules&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Must Be Member&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;=&lt;/code&gt; (assignment), &lt;code&gt;[]&lt;/code&gt; (subscript), &lt;code&gt;()&lt;/code&gt; (call), &lt;code&gt;-&amp;gt;&lt;/code&gt; (member access arrow)&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Required by the C++ language standard to be declared &lt;strong&gt;exclusively as member functions&lt;/strong&gt; (declaring them as nonmembers is a compile error).&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Should Be Member&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;+=&lt;/code&gt;, &lt;code&gt;-=&lt;/code&gt;, &lt;code&gt;*=&lt;/code&gt; (compound assignment)&lt;/strong&gt;&lt;br&gt;&lt;strong&gt;&lt;code&gt;++&lt;/code&gt;, &lt;code&gt;--&lt;/code&gt; (increment/decrement), &lt;code&gt;*&lt;/code&gt; (dereference)&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Directly mutate the object&amp;rsquo;s state or are unary operations tightly bound to the class type.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Should Be Nonmember&lt;/strong&gt;&lt;br&gt;&lt;code&gt;friend&lt;/code&gt; if needed&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;&lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt; (arithmetic)&lt;/strong&gt;&lt;br&gt;&lt;strong&gt;&lt;code&gt;==&lt;/code&gt;, &lt;code&gt;!=&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt; (equality/relational)&lt;/strong&gt;&lt;br&gt;&lt;strong&gt;&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; (I/O streams)&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Symmetry &amp;amp; Left-Hand Implicit Conversion:&lt;/strong&gt; A member operator requires the left-hand operand to be &lt;code&gt;*this&lt;/code&gt;. If &lt;code&gt;operator+&lt;/code&gt; were a member of &lt;code&gt;std::string&lt;/code&gt;, &lt;code&gt;STRING_INSTANCE + &amp;quot;hi&amp;quot;&lt;/code&gt; would work, but &lt;strong&gt;&lt;code&gt;&amp;quot;hi&amp;quot; + STRING_INSTANCE&lt;/code&gt; would fail to compile because the left operand is &lt;code&gt;const char*&lt;/code&gt;&lt;/strong&gt;! Defining symmetric operators as nonmembers allows implicit conversions on both left and right operands equally.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;hr&gt;
&lt;h2 id="2-implementation-idioms-by-operator"&gt;2. Implementation Idioms by Operator&lt;a class="anchor" href="#2-implementation-idioms-by-operator"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;table&gt;
 &lt;thead&gt;
 &lt;tr&gt;
 &lt;th style="text-align: left"&gt;Operator&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Signature Pattern&lt;/th&gt;
 &lt;th style="text-align: left"&gt;Key Implementation Rules&lt;/th&gt;
 &lt;/tr&gt;
 &lt;/thead&gt;
 &lt;tbody&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Output (&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream &amp;amp;os, const CLASS &amp;amp;x);&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Must be a &lt;strong&gt;nonmember function&lt;/strong&gt; — defining it as a member of &lt;code&gt;CLASS&lt;/code&gt; cannot match the &lt;code&gt;cout &amp;lt;&amp;lt; CLASS_INSTANCE&lt;/code&gt; syntax. Minimize formatting and return &lt;code&gt;os&lt;/code&gt;.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Input (&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;istream&amp;amp; operator&amp;gt;&amp;gt;(istream &amp;amp;is, CLASS &amp;amp;x);&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Nonmember function. &lt;strong&gt;Input can fail at runtime, so always check &lt;code&gt;if (is)&lt;/code&gt; after reading&lt;/strong&gt;, and if it fails, reset &lt;code&gt;x = CLASS();&lt;/code&gt; to leave the object in a valid default state.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Arithmetic (&lt;code&gt;+&lt;/code&gt;) vs. Compound (&lt;code&gt;+=&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;CLASS&amp;amp; CLASS::operator+=(const CLASS &amp;amp;rhs);&lt;/code&gt;&lt;br&gt;&lt;code&gt;CLASS operator+(const CLASS &amp;amp;lhs, const CLASS &amp;amp;rhs);&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Implement &lt;code&gt;operator+=&lt;/code&gt; as a member function first, then implement nonmember &lt;code&gt;operator+&lt;/code&gt; by copying &lt;code&gt;lhs&lt;/code&gt;, delegating to &lt;code&gt;+= rhs&lt;/code&gt;, and returning by value &lt;code&gt;CLASS&lt;/code&gt;.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Subscript (&lt;code&gt;[]&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;ELEM&amp;amp; operator[](size_t n);&lt;/code&gt;&lt;br&gt;&lt;code&gt;const ELEM&amp;amp; operator[](size_t n) const;&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Always define &lt;strong&gt;both non-&lt;code&gt;const&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; versions&lt;/strong&gt;. This allows modifying elements on non-const objects while still reading elements on &lt;code&gt;const&lt;/code&gt; objects.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Prefix vs. Postfix (&lt;code&gt;++&lt;/code&gt;, &lt;code&gt;--&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Prefix:&lt;/strong&gt; &lt;code&gt;CLASS&amp;amp; operator++();&lt;/code&gt;&lt;br&gt;&lt;strong&gt;Postfix:&lt;/strong&gt; &lt;code&gt;CLASS operator++(int);&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Postfix takes an &lt;strong&gt;unused dummy &lt;code&gt;int&lt;/code&gt; parameter&lt;/strong&gt; solely to distinguish its signature from prefix. Prefix returns an lvalue reference &lt;code&gt;CLASS&amp;amp;&lt;/code&gt; to the incremented &lt;code&gt;*this&lt;/code&gt;; postfix saves a copy of the old state, calls &lt;code&gt;++*this&lt;/code&gt;, and returns the old copy by value &lt;code&gt;CLASS&lt;/code&gt;.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;tr&gt;
 &lt;td style="text-align: left"&gt;&lt;strong&gt;Member Access (&lt;code&gt;*&lt;/code&gt;, &lt;code&gt;-&amp;gt;&lt;/code&gt;)&lt;/strong&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;&lt;code&gt;T&amp;amp; operator*() const;&lt;/code&gt;&lt;br&gt;&lt;code&gt;T* operator-&amp;gt;() const;&lt;/code&gt;&lt;/td&gt;
 &lt;td style="text-align: left"&gt;Used in smart pointers and iterators. For an object &lt;code&gt;CLASS_INSTANCE&lt;/code&gt;, &lt;code&gt;CLASS_INSTANCE-&amp;gt;MEMBER&lt;/code&gt; is evaluated as &lt;code&gt;(CLASS_INSTANCE.operator-&amp;gt;())-&amp;gt;MEMBER&lt;/code&gt;, so &lt;code&gt;operator-&amp;gt;()&lt;/code&gt; must return a raw pointer or another object that defines &lt;code&gt;operator-&amp;gt;&lt;/code&gt;.&lt;/td&gt;
 &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;// Prefix vs. Postfix Increment Implementation
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;class&lt;/span&gt; &lt;span style="color:#1f2328"&gt;Ptr&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;public&lt;/span&gt;&lt;span style="color:#0550ae"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Ptr&lt;span style="color:#0550ae"&gt;&amp;amp;&lt;/span&gt; &lt;span style="color:#cf222e"&gt;operator&lt;/span&gt;&lt;span style="color:#0550ae"&gt;++&lt;/span&gt;&lt;span style="color:#1f2328"&gt;()&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Prefix (++p): increment and return reference to *this
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#0550ae"&gt;++&lt;/span&gt;curr&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;return&lt;/span&gt; &lt;span style="color:#0550ae"&gt;*&lt;/span&gt;&lt;span style="color:#cf222e"&gt;this&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#1f2328"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Ptr &lt;span style="color:#cf222e"&gt;operator&lt;/span&gt;&lt;span style="color:#0550ae"&gt;++&lt;/span&gt;&lt;span style="color:#1f2328"&gt;(&lt;/span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt;&lt;span style="color:#1f2328"&gt;)&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Postfix (p++): dummy int parameter! Save old state, call prefix, return old copy
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; Ptr ret &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0550ae"&gt;*&lt;/span&gt;&lt;span style="color:#cf222e"&gt;this&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#0550ae"&gt;++*&lt;/span&gt;&lt;span style="color:#cf222e"&gt;this&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Reuse prefix operator++
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;return&lt;/span&gt; ret&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#1f2328"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;private&lt;/span&gt;&lt;span style="color:#0550ae"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; &lt;span style="color:#0550ae"&gt;*&lt;/span&gt;curr&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id="3-function-call-operator-operator-and-functors"&gt;3. Function-Call Operator &lt;code&gt;operator()&lt;/code&gt; and Functors&lt;a class="anchor" href="#3-function-call-operator-operator-and-functors"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When a class overloads the function-call operator &lt;code&gt;operator()&lt;/code&gt;, instances of that class can be invoked like functions. Such classes are called &lt;strong&gt;Function Objects&lt;/strong&gt; or &lt;strong&gt;Functors&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Object-Oriented Programming (OOP)</title><link>/programming/cpp/primer/oop/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/oop/</guid><description>&lt;h1 id="object-oriented-programming-oop"&gt;Object-Oriented Programming (OOP)&lt;a class="anchor" href="#object-oriented-programming-oop"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt;, Chapter 15: Object-Oriented Programming and Section 19.2.1: &lt;code&gt;dynamic_cast&lt;/code&gt;, this post summarizes the three pillars of OOP, &lt;strong&gt;Upcast / Downcast, Object Slicing, and Polymorphic Containers&lt;/strong&gt;, &lt;strong&gt;Virtual Functions&lt;/strong&gt; (&lt;code&gt;virtual&lt;/code&gt;, &lt;code&gt;override&lt;/code&gt;, &lt;code&gt;final&lt;/code&gt;), &lt;strong&gt;Pure Virtual Functions&lt;/strong&gt; (&lt;code&gt;= 0&lt;/code&gt;) and &lt;strong&gt;Abstract Base Classes&lt;/strong&gt;, and access control under inheritance.&lt;/p&gt;
&lt;h2 id="1-the-three-pillars-of-oop"&gt;1. The Three Pillars of OOP&lt;a class="anchor" href="#1-the-three-pillars-of-oop"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Object-oriented programming is built on three core principles:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Data Abstraction:&lt;/strong&gt; Define classes that separate interface from implementation. — see &lt;a href="../../programming/cpp/primer/classes/"&gt;Classes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inheritance:&lt;/strong&gt; Define classes that model the relationships among similar types.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic Binding:&lt;/strong&gt; Use objects of derived types while ignoring the details of how they differ.&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id="2-upcast-vs-downcast-and-static-vs-dynamic-type"&gt;2. Upcast vs. Downcast and Static vs. Dynamic Type&lt;a class="anchor" href="#2-upcast-vs-downcast-and-static-vs-dynamic-type"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Expressions dealing with polymorphic types distinguish between two types:&lt;/p&gt;</description></item><item><title>Templates and Generic Programming</title><link>/programming/cpp/primer/templates/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/templates/</guid><description>&lt;h1 id="templates-and-generic-programming"&gt;Templates and Generic Programming&lt;a class="anchor" href="#templates-and-generic-programming"&gt;#&lt;/a&gt;&lt;/h1&gt;

&lt;p&gt;Based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt;, Chapter 16 (Templates and Generic Programming), this post summarizes function and class template instantiation, template argument deduction, &lt;strong&gt;forwarding references (&lt;code&gt;T&amp;amp;&amp;amp;&lt;/code&gt;) and reference collapsing&lt;/strong&gt;, &lt;strong&gt;variadic templates (&lt;code&gt;Args...&lt;/code&gt;) and how &lt;code&gt;emplace_back&lt;/code&gt; works under the hood&lt;/strong&gt;, and template specialization.&lt;/p&gt;
&lt;h2 id="1-function-and-class-template-fundamentals"&gt;1. Function and Class Template Fundamentals&lt;a class="anchor" href="#1-function-and-class-template-fundamentals"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Template Instantiation:&lt;/strong&gt; A template itself is a &lt;strong&gt;blueprint&lt;/strong&gt;, not executable code. When you invoke &lt;code&gt;compare(1, 0)&lt;/code&gt; or declare &lt;code&gt;std::vector&amp;lt;std::string&amp;gt;&lt;/code&gt;, the compiler substitutes the concrete type for &lt;code&gt;T&lt;/code&gt; and instantiates a type-specific version of the function or class.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Why Must Template Declarations and Definitions Live in Header Files (&lt;code&gt;.h&lt;/code&gt;)?&lt;/strong&gt;
&lt;ul&gt;
&lt;li&gt;For an ordinary function, the compiler only needs the declaration to compile a call site, and the linker later links the &lt;code&gt;.o&lt;/code&gt; object file. &lt;strong&gt;To instantiate a template, however, the compiler must see the full template body at the call site&lt;/strong&gt; so it can generate code for the concrete type.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="1-type-parameters-typename--class-and-non-type-template-parameters"&gt;1. Type Parameters (&lt;code&gt;typename&lt;/code&gt; / &lt;code&gt;class&lt;/code&gt;) and Non-type Template Parameters&lt;a class="anchor" href="#1-type-parameters-typename--class-and-non-type-template-parameters"&gt;#&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In addition to type parameters (&lt;code&gt;typename T&lt;/code&gt;), templates can take &lt;strong&gt;non-type template parameters&lt;/strong&gt; representing compile-time constant expressions (&lt;code&gt;constexpr&lt;/code&gt; integers, pointers, or references):&lt;/p&gt;</description></item><item><title>Namespaces</title><link>/programming/cpp/primer/namespaces/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/namespaces/</guid><description>&lt;h1 id="namespaces"&gt;Namespaces&lt;a class="anchor" href="#namespaces"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Summary based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Section 18.2 (Namespaces) and Modern C++ (C++17) best practices.&lt;/p&gt;
&lt;h2 id="1-purpose-and-basic-syntax"&gt;1. Purpose and Basic Syntax&lt;a class="anchor" href="#1-purpose-and-basic-syntax"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Namespaces partition the global scope into distinct logical scopes to prevent name collisions (&lt;strong&gt;Global Namespace Pollution&lt;/strong&gt;) when combining multiple libraries in large-scale programs.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No Trailing Semicolon:&lt;/strong&gt; Unlike class definitions (&lt;code&gt;class Foo { ... };&lt;/code&gt;), a namespace block does &lt;strong&gt;not&lt;/strong&gt; end with a semicolon (&lt;code&gt;;&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Discontiguous Definitions:&lt;/strong&gt; A namespace can be split across multiple files (&lt;code&gt;.h&lt;/code&gt; and &lt;code&gt;.cc&lt;/code&gt;) or blocks. Reopening an existing namespace adds declarations to it rather than creating a new one.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scope Operator (&lt;code&gt;::&lt;/code&gt;):&lt;/strong&gt; Members are accessed via &lt;code&gt;ns::name&lt;/code&gt;. Writing a leading &lt;code&gt;::name&lt;/code&gt; with no prefix explicitly refers to &lt;code&gt;name&lt;/code&gt; in the &lt;strong&gt;global namespace&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nested Namespaces:&lt;/strong&gt; Namespaces can be nested inside one another; since &lt;strong&gt;C++17&lt;/strong&gt;, nested namespaces can be written concisely as &lt;code&gt;namespace a::b::c { ... }&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="background-color:#f7f7f7;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-cpp" data-lang="cpp"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;int&lt;/span&gt; version &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0550ae"&gt;1&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Global namespace (::version)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;// Classic nested declaration (pre-C++17)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;namespace&lt;/span&gt; my_lib &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;namespace&lt;/span&gt; internal &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; version &lt;span style="color:#0550ae"&gt;=&lt;/span&gt; &lt;span style="color:#0550ae"&gt;2&lt;/span&gt;&lt;span style="color:#1f2328"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt; &lt;span style="color:#57606a"&gt;// No semicolon needed
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#57606a"&gt;// C++17 nested namespace shorthand (equivalent)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;namespace&lt;/span&gt; my_lib&lt;span style="color:#0550ae"&gt;::&lt;/span&gt;net &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;class&lt;/span&gt; &lt;span style="color:#1f2328"&gt;Client&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#cf222e"&gt;public&lt;/span&gt;&lt;span style="color:#0550ae"&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;int&lt;/span&gt; GetGlobalVersion&lt;span style="color:#1f2328"&gt;()&lt;/span&gt; &lt;span style="color:#cf222e"&gt;const&lt;/span&gt; &lt;span style="color:#1f2328"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#cf222e"&gt;return&lt;/span&gt; &lt;span style="color:#0550ae"&gt;::&lt;/span&gt;version&lt;span style="color:#1f2328"&gt;;&lt;/span&gt; &lt;span style="color:#57606a"&gt;// Explicitly accesses global ::version (1)
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#1f2328"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;};&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#1f2328"&gt;}&lt;/span&gt; &lt;span style="color:#57606a"&gt;// namespace my_lib::net
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;h2 id="2-unnamed-anonymous-namespaces-and-internal-linkage"&gt;2. Unnamed (Anonymous) Namespaces and Internal Linkage&lt;a class="anchor" href="#2-unnamed-anonymous-namespaces-and-internal-linkage"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A block declared as &lt;code&gt;namespace { ... }&lt;/code&gt; without a name is an &lt;strong&gt;unnamed (anonymous) namespace&lt;/strong&gt;.&lt;/p&gt;</description></item><item><title>Pointers to Class Member</title><link>/programming/cpp/primer/pointers-to-class-member/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>/programming/cpp/primer/pointers-to-class-member/</guid><description>&lt;h1 id="pointers-to-class-member"&gt;Pointers to Class Member&lt;a class="anchor" href="#pointers-to-class-member"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;Summary based on &lt;em&gt;C++ Primer, 5th ed.&lt;/em&gt; Section 19.4: Pointers to Class Member, paired with modern C++ &lt;code&gt;std::invoke&lt;/code&gt; and C++20 Ranges projections.&lt;/p&gt;
&lt;h2 id="1-ordinary-pointers-vs-pointers-to-class-member---"&gt;1. Ordinary Pointers vs. Pointers to Class Member (&lt;code&gt;.*&lt;/code&gt;, &lt;code&gt;-&amp;gt;*&lt;/code&gt;)&lt;a class="anchor" href="#1-ordinary-pointers-vs-pointers-to-class-member---"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;While an ordinary pointer &lt;code&gt;TYPE*&lt;/code&gt; stores the &lt;strong&gt;actual memory address of a specific live object&lt;/strong&gt;, a &lt;strong&gt;pointer to member &lt;code&gt;TYPE CLASS::*&lt;/code&gt;&lt;/strong&gt; stores no object address at all — it identifies &lt;strong&gt;which non-&lt;code&gt;static&lt;/code&gt; member within &lt;code&gt;CLASS&lt;/code&gt; to select&lt;/strong&gt;, such as its relative byte offset inside &lt;code&gt;CLASS&lt;/code&gt;.&lt;/p&gt;</description></item></channel></rss>