intx1(5.3);//OK int x2 = 5.3;//OK int x3{5.3}; //error: narrowing conversion of ‘5.2999999999999998e+0’ from ‘double’ to ‘int’ inside { } [-Wnarrowing] int x4 = {5.3}; // error: narrowing conversion of ‘5.2999999999999998e+0’ from ‘double’ to ‘int’ inside { } [-Wnarrowing] char c1 {7}; //OK char c2{257}; //error: narrowing conversion of ‘257’ from ‘int’ to ‘char’ inside { } [-Wnarrowing] std::vector<int> v1 {1,2,3,4,5};//OK std::vector<int> v2{1,2.3,4,4.6};/ error: narrowing conversion of ‘2.2999999999999998e+0’ from ‘double’ to ‘int’ inside { } [-Wnarrowing]
classP { public: // fun1 P(int a, int b) { std::cout<< "P(int,int)"<<std::endl; } //fun2 P(std::initializer_list<int> initlist) { std::cout<<"P(initializer)"<<std::endl; } }; // fun1 与 fun2 同时存在的调用结果 P p(77, 5); // P(int,int) P q{77, 5}; //P(initializer) P r{77, 55, 26}; //P(initializer) P s = {77, 5}; //P(initializer)
// 去掉构造函数fun2 只保留fun1 P p(77, 5); // P(int,int) P q{77, 5}; //P(int,int) P r{77, 55, 26}; //error: no matching function for call to ‘P::P(<brace-enclosed initializer list>)’ P s = {77, 5}; //P(int,int)