You're correct about the overload resolution: Inside
Vector_container(int s): v{s}{}
the initialization v{s}
selects the following constructor:
Vector(std::initializer_list<double> lst)
as per [over.match.list]/1.
When the std::initializer_list<double>
is created from {s}
, where s
is of type int
, there is a narrowing conversion from int
to double
(n3485) [dcl.init.list]/7
A narrowing conversion is an implicit conversion
- [...]
- from an integer type or unscoped enumeration type to a floating-point type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type, or
- [...]
Note that s
here is not a constant expression any more (being a parameter). Narrowing conversions may not appear in the construction of an std::initializer_list
object, [dcl.init.list]/5
If a narrowing conversion is required to initialize any of the elements, the program is ill-formed.
So that warning should be an error (or it's an extension).
Thus, I wonder if there's any way to impose the invoking of the
Vector(int)
constructor, instead of the initializer-list ctor in theVector
class.
I'm not sure if I understood you correctly (see comments to OP), but not using list-init here solves the problem:
Vector_container(int s): v(s) {} // initializer-list ctor not viable