list-initialization

Why doesn't C++11 curly brace initialzation in constructor initialization list work when parens initializaton does?

拜拜、爱过 提交于 2020-01-01 04:21:46
问题 How is {} initialization in a constructor initialization list different from () initialization when initializing reference to abstract types? Take class Bar below: class AbstractBase { public: AbstractBase() {} virtual ~AbstractBase() = default; virtual void ab() = 0; }; class Foo : public AbstractBase { public: Foo() {} void ab() {} }; class Bar { public: Bar(const AbstractBase& base) : myBase{base} {} private: const AbstractBase& myBase; }; int main() { Foo f{}; Bar b{f}; } When compiling,

List-initialization and failed overload resolution of initializer_list constructor

你。 提交于 2019-12-31 21:45:19
问题 The below fails to compile with clang35 -std=c++11 : #include <iostream> #include <string> #include <initializer_list> class A { public: A(int, bool) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(int, double) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(std::initializer_list<int>) { std::cout << __PRETTY_FUNCTION__ << std::endl; } }; int main() { A a1 = {1, 1.0}; return 0; } with error init.cpp:15:14: error: type 'double' cannot be narrowed to 'int' in initializer list [-Wc++11

Problem with calling a variadic function template when passing brace initialiser list arguments

风流意气都作罢 提交于 2019-12-30 03:56:05
问题 Consider this function template: template <class... T> void foo (std::tuple<T, char, double> ... x); This invocation works: using K = std::tuple<int, char, double>; foo ( K{1,'2',3.0}, K{4,'5',6.0}, K{7,'8',9.0} ); This one doesn't: foo ( {1,'2',3.0}, {4,'5',6.0}, {7,'8',9.0} ); (gcc and clang both complain about too many arguments for foo ) Why is the second call a problem? Can I rewrite the declaration of foo so that the second call is also accepted? Thee template parameter T is only used

Is it possible to invoke a user-defined conversion function via list-initialization?

女生的网名这么多〃 提交于 2019-12-28 04:20:55
问题 Is this program legal? struct X { X(const X &); }; struct Y { operator X() const; }; int main() { X{Y{}}; // ?? error } After n2672, and as amended by defect 978, 13.3.3.1 [over.best.ics] has: 4 - However, when considering the argument of a constructor or user-defined conversion function that is a candidate [...] by 13.3.1.7 [...] when the initializer list has exactly one element and a conversion to some class X or reference to (possibly cv-qualified) X is considered for the first parameter

why this variable isn't deduced as initializer_list in g++ in C++14?

拜拜、爱过 提交于 2019-12-24 15:04:54
问题 Consider the following program: #include <iostream> int main() { int n = 3; int fact = 1; for(auto i{1};i<=n;i++) fact*=i; std::cout<<"fact of "<<n<<" is "<<fact; } It compiles fine on ideone even when I use -std=c++14 option. See live demo here. But in C++14 the variable i should be deduced as initializer_list according to this. There is a proposal for C++1z that implements new type deduction rules for brace initialization: For direct list-initialization: For a braced-init-list with only a

Why is this initializer_list constructor a viable overload?

一曲冷凌霜 提交于 2019-12-24 05:12:31
问题 #include <iostream> #include <string> #include <initializer_list> class A { public: A(int, bool) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(int, double) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(std::initializer_list<int>) { std::cout << __PRETTY_FUNCTION__ << std::endl; } }; int main() { A a1 = {1, 1.0}; return 0; } (This question is a follow-up to this.) The above program fails to compile with clang35 -std=c++11 init.cpp:15:14: error: type 'double' cannot be narrowed to

Why is this initializer_list constructor a viable overload?

笑着哭i 提交于 2019-12-24 05:12:06
问题 #include <iostream> #include <string> #include <initializer_list> class A { public: A(int, bool) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(int, double) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(std::initializer_list<int>) { std::cout << __PRETTY_FUNCTION__ << std::endl; } }; int main() { A a1 = {1, 1.0}; return 0; } (This question is a follow-up to this.) The above program fails to compile with clang35 -std=c++11 init.cpp:15:14: error: type 'double' cannot be narrowed to

Is This Actually Ambiguous?

萝らか妹 提交于 2019-12-23 10:59:33
问题 So I am aware that braces in code can mean more than just an initializer_list : What Is a Curly-Brace Enclosed List If Not an intializer_list? But what should they default to? For example, say that I define an overloaded function: void foo(const initializer_list<int>& row_vector) { cout << size(row_vector) << "x1 - FIRST\n"; } void foo(const initializer_list<initializer_list<int>>& matrix) { cout << size(matrix) << 'x' << size(*begin(matrix)) << " - SECOND\n"; } If I call foo({ 1, 2, 3 }) the

Is This Actually Ambiguous?

北慕城南 提交于 2019-12-23 10:59:29
问题 So I am aware that braces in code can mean more than just an initializer_list : What Is a Curly-Brace Enclosed List If Not an intializer_list? But what should they default to? For example, say that I define an overloaded function: void foo(const initializer_list<int>& row_vector) { cout << size(row_vector) << "x1 - FIRST\n"; } void foo(const initializer_list<initializer_list<int>>& matrix) { cout << size(matrix) << 'x' << size(*begin(matrix)) << " - SECOND\n"; } If I call foo({ 1, 2, 3 }) the

Why can't a 2D std::array be initialized with two layers of list-initializers?

蹲街弑〆低调 提交于 2019-12-22 04:06:17
问题 can someone help me understand why my compiler can't/doesn't deduce this? (using g++ 7.3) Does not work: #include <array> std::array<std::array<double,2>,2> f() { return {{0,0},{0,0}}; } Works fine: #include <array> std::array<std::array<double,2>,2> f() { return {std::array<double,2>{0,0},{0,0}}; } Also weirdly this fails too: #include <array> std::array<std::array<double,2>,2> f() { return std::array<std::array<double,2>,2>{{0,0},{0,0}}; } @1201ProgramAlarm pointed out that adding another