Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To be honest, with your samples I see little wrong.</p> <p>However, there are a number of cases where the compiler seems to accept 'violations' of the standard conversion rules...:</p> <h3>Initializer lists (§ 8.5.4)</h3> <p>However I spotted this one in the standard: </p> <p>For initialzer lists, the following is not allowed (§ 8.5.4, under <strong>3.</strong>)</p> <pre><code>int ai[] = { 1, 2.0 }; // error narrowing </code></pre> <p>Under <strong>6.</strong> it goes on to give a general list of examples:</p> <blockquote> <p><em>[ Note: As indicated above, such conversions are not allowed at the top level in list-initializations.—end note ]</em></p> </blockquote> <pre><code>int x = 999; // x is not a constant expression const int y = 999; const int z = 99; char c1 = x; // OK, though it might narrow (in this case, it does narrow) char c2{x}; // error: might narrow char c3{y}; // error: narrows (assuming char is 8 bits) char c4{z}; // OK: no narrowing needed unsigned char uc1 = {5}; // OK: no narrowing needed unsigned char uc2 = {-1}; // error: narrows unsigned int ui1 = {-1}; // error: narrows signed int si1 = { (unsigned int)-1 }; // error: narrows int ii = {2.0}; // error: narrows float f1 { x }; // error: might narrow float f2 { 7 }; // OK: 7 can be exactly represented as a float int f(int); int a[] = { 2, f(2), f(2.0) }; // OK: the double-to-int conversion is not at the top level </code></pre> <p>Interestingly, g++ 4.6.1 with <code>--std=c++0x -Wall -pedantic</code> catches <strong>only one</strong> of these violations:</p> <pre><code> char c3{y}; // warning: overflow in implicit constant conversion [-Woverflow] </code></pre> <hr> <h3>Outside initializer lists...</h3> <p>I don't think the truncation of a float to an int is considered <code>narrowing</code>. </p> <p>It is just a well-defined conversion, much like</p> <pre><code>int i = 31; i /= 4; // well defined loss of precision... i /= 4.0; // equally well-defined conversion from floating point to int </code></pre>
    singulars
    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. 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