Quick A: Yes, but some compilers have conforming extensions that give meaning to nonportable programs.
Preventing narrowing conversion when using std::initializer_list
#include <iostream> struct X { X(std::initializer_list<int> list) { std::cout << "list" << std::endl; } X(float f) { std::cout << "float" << std::endl; } }; int main() { int x { 1.0f }; X a(1); // float (implicit conversion) X b{1}; // list X c(1.0f); // float X d{1.0f}; // list (narrowing conversion) ARG!!! // warning: narrowing conversion of '1.0e+0f' from 'float' to 'int' // inside { } [-Wnarrowing] }Is there any other way of removing
std::initializer_list
from an overload list (i.e., making the non-list ctors more favorable) instead of using the()
-initialization, or at least prohibiting narrowing conversion to happen (apart from turning warning into error)?I was using http://coliru.stacked-crooked.com/ compiler which uses GCC 4.8.
Add a Comment
Comments are closed.