list-initialization

Why would they special-case certain initializer lists instead of treating them all the same?

旧城冷巷雨未停 提交于 2019-12-05 18:31:27
Say I have a variable auto x that I want to initialize to 7 using brace initialization, simple: auto x {7}; Except I learned that x is NOT an integer, but an initialization list itself. Why? Is there a specific reason why the committee would decide that auto should grab the initialization list in the case of a single auto value, or do they expect us to just realize these shouldn't be used together. I cant seem to think of a possible reason i would want an initializer list to be stored into auto as opposed to the value A very practical answer is, "why should int be selected?" Or double , or any

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

倖福魔咒の 提交于 2019-12-05 03:27:10
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 set of curly braces works: #include <array> std::array<std::array<double,2>,2> f() { return {{{0,0},{0,0

Why does direct list initialization causes ambiguity for type reference cast if cast operators to the type and reference to the type are declared?

久未见 提交于 2019-12-05 01:20:38
The question rose in context of this answer . Consider an example: struct foo { int value; operator int&(){ return value; } operator int(){ return value; } }; int main () { int &a(foo{}); // #1 //int &b{foo{}}; // #2 -- ambiguity int &c = foo{}; // #3 //int &d = {foo{}}; // #4-- ambiguity int &d { a }; // #5 int &e = { a }; // #6 (void)a; (void)c; (void)d; (void)e; } I don't understand why does #2 and #4 cause ambiguity while #1 and #3 does not. So the question is - why does direct list initialization causes ambiguity for implicit cast to reference if cast operators to the type and reference

MSVC brace initialization with doubles appears to violate the standard?

妖精的绣舞 提交于 2019-12-04 02:56:56
问题 Check out this simple program: int main() { float f2 = 7.2; // OK, with warning float f3 = 7.199999809265137; // OK, no warning float f4{ 7.2 }; // Fails float f5{ 7.199999809265137 }; // OK, no warning float f6 = { 7.2 }; // Fails float f7 = { 7.199999809265137 }; // OK, no warning } When compiled with MSVC 2015 using the default options ( cl /W4 , version 19.00.23918), I get the following messages: FloatTest.cpp(2): warning C4305: 'initializing': truncation from 'double' to 'float'

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

那年仲夏 提交于 2019-12-03 11:05:20
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, I get the error test5.cpp: In constructor ‘Bar::Bar(const AbstractBase&)’: test5.cpp:22:48: error:

Why does GCC 6.3 compile this Braced-Init-List code without explicit C++11 support?

巧了我就是萌 提交于 2019-12-03 10:55:34
I have a question about the different meanings of a curly-brace enclosed list . I know that C++03 did not support C++11's initializer_list . Yet, even without the -std=c++11 compiler flag, gcc 6.3 will properly initialize interpolate with this code: map<string, string> interpolate = { { "F", "a && b && c" }, { "H", "p ^ 2 + w" }, { "K", "H > 10 || e < 5" }, { "J", "F && !K" } }; I was challenged on why this would work, and I realized I didn't have an answer. This is a Brace-Init-List, but the way we get from that to initializing a standard container is typically through an initializer_list .

List-initialization and failed overload resolution of initializer_list constructor

Deadly 提交于 2019-12-03 02:42:22
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-narrowing] A a1 = {1, 1.0}; ^~~ init.cpp:15:14: note: insert an explicit cast to silence this issue A

Initializing scalars with braces

孤人 提交于 2019-12-01 15:19:27
In C and C++, one can initialize arrays and structs using braces: int a[] = {2, 3, 5, 7}; entry e = {"answer", 42}; However, in a talk from 2007 , Bjarne mentions that this syntax also works for scalars. I tried it: int i = {7}; And it actually works! What is the rationale behind allowing the initialization of scalars with braces? Note: I am specifically not talking about C++11 uniform initialization. This is good old C89 and C++98. What is the rationale behind allowing the initialization of scalars with braces? int is POD. So the brace initialization is allowed in case of int (and for all

Why does a narrowing conversion warning appear only in case of list initialization?

孤人 提交于 2019-11-30 17:35:48
I have the following code: class A { public: A(const unsigned int val) : value(val) {} unsigned int value; }; int main() { int val = 42; A a(val); A b{val}; // <--- Warning in GCC, error in Microsoft Visual Studio 2015 return 0; } Why does the narrowing conversion warning appear only in case of list initialization usage? list initialization was introduced since C++11 with the feature prohibiting implicit narrowing conversions among built-in types. At the same time, the other two "old-style" (since C++98) initialization forms which use parentheses and equal-sign like int val = 42; A a(val); A a

Are ={} and {}-style initializations the same in C++11?

与世无争的帅哥 提交于 2019-11-30 11:57:40
C++11 introduced {}-style initializations. But are these two forms T x {...}; T x = {...}; the same? They are not exactly the same. Maybe this can be illustrated by a counter-example: struct Foo { explicit Foo(std::initializer_list<int>) {} }; int main() { Foo f0{1, 2, 3}; // OK Foo f1 = {1, 2, 3}; // ERROR } So, the second variant requires that the type be implicitly constructable from an initialization list, whereas the first version doesn't. Note that the same applies for constructors of the form Foo(int, int, int) . I chose the initializer_list<int> as an example arbitrarily. This would