<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Cast on Cheat Sheet</title><link>/tags/cast/</link><description>Recent content in Cast on Cheat Sheet</description><generator>Hugo</generator><language>en</language><atom:link href="/tags/cast/index.xml" rel="self" type="application/rss+xml"/><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></channel></rss>