Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are casting <em>from</em> a numeric type, <em>to</em> another numeric type, then I think C-style casts are <strong>preferable</strong> to <code>*_cast</code>. Each of the <code>*_cast</code> operators has a specific reason not to use it on numeric types:</p> <ul> <li><code>reinterpret_cast</code>, when applied to numeric types, does a normal numeric conversion rather than reinterpreting the bits, i.e. writing <code>reinterpret_cast&lt;uint64_t&gt;(3.14159)</code> <em>does not</em> produce the integer with the same bit representation as that floating-point constant. This is contrary to intuition.</li> <li><code>const_cast</code> is never necessary on a numeric <em>value</em>, and if applied to a numeric <em>variable</em> (as opposed to a pointer or reference to a number), suggests that the type of that variable is incorrect.</li> <li><code>dynamic_cast</code> just plain doesn't make sense on numbers because numbers never have dynamic type.</li> <li><code>static_cast</code> is normally used on class types. Therefore it <em>looks strange</em> to apply <code>static_cast</code> to a number; your readers will scratch their heads and wonder if there's something they don't know about <code>static_cast</code> that makes it <em>different</em> from the C-style cast when applied to a number. (In fact, they are identical, but I had to read the relevant C++ spec section a couple times to be <em>sure</em> they were identical, and I still have the "something weird must be going on here" reaction.)</li> </ul> <p>and there is an additional stylistic reason to avoid them:</p> <ul> <li><p>If you need to cast among numeric types, you are likely to need to do several of them in a cluster; because of that, the concision of C-style casts is important. For instance, the program I'm writing right now has a whole bunch of this sort of thing:</p> <pre><code>uint32_t n = ((uint32_t(buf[0]) &lt;&lt; 24) | (uint32_t(buf[1]) &lt;&lt; 16) | (uint32_t(buf[2]) &lt;&lt; 8) | (uint32_t(buf[3]) )); </code></pre> <p>Using <code>static_cast</code> here would obscure the arithmetic, which is the important thing. (Note: those casts are unnecessary only if <code>sizeof(uint32_t) &lt;= sizeof(unsigned int)</code>, which <em>is</em> a safe assumption nowadays but I still prefer the explicitness.) (Yes, I probably ought to factor out this operation into a <code>be32_to_cpu</code> inline helper, but I'd still code it the same way.)</p></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload