uniform-initialization

Why can't I initialize a reference in an initializer list with uniform initialization?

守給你的承諾、 提交于 2019-11-26 17:45:08
问题 That is, why does this: struct S {}; struct T { T(S& s) : s{s} {} S& s; }; int main() { S s; T t{s}; } give me a compiler error with GCC 4.7: test.cpp: In constructor 'T::T(S&)': test.cpp:5:18: error: invalid initialization of non-const reference of type 'S&' from an rvalue of type '<brace-enclosed initializer list>' ? To fix the error, I have to change the s{s} to s(s) . Doesn't this break the, erm, uniformity of uniform initialization? EDIT : I tried with clang, and clang accepts it, so

Uniform initialization fails to copy when object has no data members

回眸只為那壹抹淺笑 提交于 2019-11-26 14:48:53
问题 In updating some code to use uniform initialization, I thought it would be a drop-in modern substitue for the now "old style" parentheses style. I know this isn't always the case (obvious example, vector<int> ) but I've stumbled over another difference that I don't understand. class Object { public: Object() = default; Object(const Object&) = default; }; int main() { Object o; Object copy{o}; // error Object copy2(o); // OK } fails to compile under clang3.5 with the error: (also fails under

Why does &#39;std::vector<int> b{2};&#39; create a 1-element vector, and not a 2-element one?

泪湿孤枕 提交于 2019-11-26 14:12:42
问题 I've been playing around with C++11 for the past few days, and I came up with something strange. If I want to uniformly initialize an int: int a{5}; But if I do the same thing to a std::vector: std::vector<int> b{2}; Does not construct a two element array, but rather an array with one element of value two. It seems like to get that effect one would need to be more explicit about it: std::vector<int> c{{2}}; std::vector<int> d = {2}; But not like the declaration of b - this seems inconsistent.

Why doesn&#39;t emplace_back() use uniform initialization?

只谈情不闲聊 提交于 2019-11-26 10:31:23
问题 The following code: #include <vector> struct S { int x, y; }; int main() { std::vector<S> v; v.emplace_back(0, 0); } Gives the following errors when compiled with GCC: In file included from c++/4.7.0/i686-pc-linux-gnu/bits/c++allocator.h:34:0, from c++/4.7.0/bits/allocator.h:48, from c++/4.7.0/vector:62, from test.cpp:1: c++/4.7.0/ext/new_allocator.h: In instantiation of \'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = S; _Args = {int, int}; _Tp = S]\': c++/4.7.0

How to use C++11 uniform initialization syntax?

眉间皱痕 提交于 2019-11-26 07:45:37
问题 I cannot understand when and how to use the new uniform initialization syntax in C++11. For example, I get this: std::string a{\"hello world\"}; // OK std::string b{a}; // NOT OK Why does it not work in the second case? The error is: error: no matching function for call to ‘std::basic_string<char>::basic_string(<brace enclosed initializer list>)’ with this version of g++ g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 . And with primitive data, what syntax should I use? int i = 5; int i{5}; int i =