list-initialization

How should I brace-initialize an std::array of std::pairs?

坚强是说给别人听的谎言 提交于 2019-11-27 08:37:42
std::array<std::pair<int, int>, 2> ids = { { 0, 1 }, { 1, 2 } }; VS2013 error: error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the source type, or constructor overload resolution was ambiguous` What am I doing wrong? Add another pair of braces. std::array<std::pair<int, int>, 2> ids = { { { 0, 1 }, { 1, 2 } } }; std::array<T, N> is an aggregate class containing a member of type T[N] . Usually, you can initialise that the same way you would a plain T[N] array, but when you're dealing with a non-aggregate element type, you may need to be more

Why does the standard differentiate between direct-list-initialization and copy-list-initialization?

天涯浪子 提交于 2019-11-27 06:37:47
We know that T v(x); is called direct-initialization , while T v = x; is called copy-initialization , meaning that it will construct a temporary T from x that will get copied / moved into v (which is most likely elided). For list-initialization, the standard differentiates between two forms, depending on the context. T v{x}; is called direct-list-initialization while T v = {x}; is called copy-list-initialization : §8.5.4 [dcl.init.list] p1 [...] List-initialization can occur in direct-initialization or copy-initialization contexts; list-initialization in a direct-initialization context is

Narrowing conversion to bool in list-initialization - strange behaviour

风格不统一 提交于 2019-11-27 01:32:02
问题 Consider this piece of C++11 code: #include <iostream> struct X { X(bool arg) { std::cout << arg << '\n'; } }; int main() { double d = 7.0; X x{d}; } There's a narrowing conversion from a double to a bool in the initialization of x . According to my understanding of the standard, this is ill-formed code and we should see some diagnostic. Visual C++ 2013 issues an error: error C2398: Element '1': conversion from 'double' to 'bool' requires a narrowing conversion However, both Clang 3.5.0 and

Deleted default constructor. Objects can still be created… sometimes

独自空忆成欢 提交于 2019-11-26 21:55:31
The naive, optimistic and oh.. so wrong view of the c++11 uniform initialization syntax I thought that since C++11 user-defined type objects should be constructed with the new {...} syntax instead of the old (...) syntax (except for constructor overloaded for std::initializer_list and similar parameters (e.g. std::vector : size ctor vs 1 elem init_list ctor)). The benefits are: no narrow implicit conversions, no problem with the most vexing parse, consistency(?). I saw no problem as I thought they are the same (except the example given). But they are not. A tale of pure madness The {} calls

C++ Copy constructor gets called instead of initializer_list<>

别等时光非礼了梦想. 提交于 2019-11-26 18:25:36
问题 Based on this code struct Foo { Foo() { cout << "default ctor" << endl; } Foo(std::initializer_list<Foo> ilist) { cout << "initializer list" << endl; } Foo(const Foo& copy) { cout << "copy ctor" << endl; } }; int main() { Foo a; Foo b(a); // This calls the copy constructor again! //Shouldn't this call the initializer_list constructor? Foo c{b}; _getch(); return 0; } The output is: default ctor copy ctor copy ctor In the third case, I'm putting b into the brace-initialization which should call

How should I brace-initialize an std::array of std::pairs?

时间秒杀一切 提交于 2019-11-26 17:46:09
问题 std::array<std::pair<int, int>, 2> ids = { { 0, 1 }, { 1, 2 } }; VS2013 error: error C2440: 'initializing' : cannot convert from 'int' to 'std::pair' No constructor could take the source type, or constructor overload resolution was ambiguous` What am I doing wrong? 回答1: Add another pair of braces. std::array<std::pair<int, int>, 2> ids = { { { 0, 1 }, { 1, 2 } } }; std::array<T, N> is an aggregate class containing a member of type T[N] . Usually, you can initialise that the same way you would

Why do auto and template type deduction differ for braced initializers?

别等时光非礼了梦想. 提交于 2019-11-26 16:50:22
问题 I understand that, given a braced initializer, auto will deduce a type of std::initializer_list , while template type deduction will fail: auto var = { 1, 2, 3 }; // type deduced as std::initializer_list<int> template<class T> void f(T parameter); f({ 1, 2, 3 }); // doesn't compile; type deduction fails I even know where this is specified in the C++11 standard: 14.8.2.5/5 bullet 5: [It's a non-deduced context if the program has] A function parameter for which the associated argument is an

lifetime of a std::initializer_list return value

╄→гoц情女王★ 提交于 2019-11-26 16:39:59
问题 GCC's implementation destroys a std::initializer_list array returned from a function at the end of the return full-expression. Is this correct? Both test cases in this program show the destructors executing before the value can be used: #include <initializer_list> #include <iostream> struct noisydt { ~noisydt() { std::cout << "destroyed\n"; } }; void receive( std::initializer_list< noisydt > il ) { std::cout << "received\n"; } std::initializer_list< noisydt > send() { return { {}, {}, {} }; }

Brace elision in std::array initialization

[亡魂溺海] 提交于 2019-11-26 16:23:36
问题 Suppose there's an std::array to be initialized. It's okay if using double braces: std::array<int, 2> x = {{0, 1}}; std::array<int, 2> x{{0, 1}}; It's also okay to use single braces in the good old aggregate initialization, as the brace elision will take care of the missing braces: std::array<int, 2> x = {0, 1}; However, is it okay to use list-initialization with single braces? GCC accepts it, Clang rejects it with "cannot omit braces around initialization of subobject when using direct list

Which greedy initializer-list examples are lurking in the Standard Library?

时光怂恿深爱的人放手 提交于 2019-11-26 16:17:27
问题 Since C++11, the Standard Library containers and std::string have constructors taking an initializer-list. This constructor takes precedence over other constructors (even, as pointed out by @JohannesSchaub-litb in the comments, even ignoring other "best match" criteria). This leads to a few well-known pitfalls when converting all parenthesized () forms of constructors to their braced versions {} #include <algorithm> #include <iostream> #include <iterator> #include <vector> #include <string>