Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Actually, a program containing a narrowing conversion in a brace list initializer is <strong>ill-formed</strong>. I am not sure why the compiler just gives you a warning, but it definitely should issue an error here (FWIW, <a href="http://coliru.stacked-crooked.com/view?id=f4b28ee252709be69fa3e506e6646409-25dabfc2c190f5ef027f31d968947336">Clang does that</a>).</p> <p>Also notice, that this is a narrowing (and therefore illegal) conversion as well:</p> <pre><code>int x { 1.0f }; // ERROR! Narrowing conversion required </code></pre> <p>Per paragraph 8.5.4/3 of the C++11 Standard:</p> <blockquote> <p>List-initialization of an object or reference of type T is defined as follows:</p> <p>— If <code>T</code> is an aggregate, aggregate initialization is performed (8.5.1). [...]</p> <p>— Otherwise, if the initializer list has no elements [...]</p> <p>— Otherwise, if <code>T</code> is a specialization of <code>std::initializer_list&lt;E&gt;</code>, [...]</p> <p>— Otherwise, if <code>T</code> is a class type, constructors are considered. The applicable constructors are enumerated and the best one is chosen through overload resolution (13.3, 13.3.1.7). <strong>If a narrowing conversion (see below) is required to convert any of the arguments, the program is ill-formed</strong>. [...]</p> </blockquote> <p>To be more precise, the Standard only says that a "diagnostic" is required in this case, and a warning is a diagnostic, so the compiler's behavior is conforming - but I believe emitting an error would be a better behavior.</p>
    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