uniform-initialization

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

老子叫甜甜 提交于 2019-11-29 17:38:49
问题 C++11 introduced {}-style initializations. But are these two forms T x {...}; T x = {...}; the same? 回答1: 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

How to uniform initialize map of unique_ptr?

白昼怎懂夜的黑 提交于 2019-11-29 13:49:54
I have this code to initialize map from into to unique_ptr. auto a = unique_ptr<A>(new A()); map<int, unique_ptr<A>> m; m[1] = move(a); Can I use uniform initialize this? I tried map<int, unique_ptr<A>> m {{1, unique_ptr<A>(new A())}}; But I got an error. Some part of error message is In instantiation of 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = {const std::pair<const int, std::unique_ptr<A, std::default_delete<A> > >&}; _Val = std::pair<const int, std::unique_ptr<A> >]': ... In file included from /opt/local/include/gcc48/c++/memory:81:0, from smart_pointer_map.cpp:3:

Copy constructor curly braces initialization

若如初见. 提交于 2019-11-29 10:34:13
"we can initializate objects of a class for which we have not define any constructor using: memberwise initialization. copy initialization. default initialization. For example: struct Work { string author; string name; int year; }; Work s9 { "Bethoven", "Symphony No. 9 in D minor, Op. 125; Choral", 1824 }; // memberwise initialization Work currently_playing {s9}; // copy initialization Work none {}; // default initialization The C++ Programming Language 4th Ed. Chapter 17.3.1 For example: struct Data { int mMember1; float mMember2; char mMember3; }; int main() { Data aData_1{1,0.3,33}; Data

c++11 array initialization won't call copy constructor

倾然丶 夕夏残阳落幕 提交于 2019-11-28 11:43:14
I'm making a little class that uses an array templated on its size. Here's some code... .hpp template <size_t N> class KeyCombinationListener { public: KeyCombinationListener( const std::array<sf::Keyboard::Key, N>& sequence, std::function<void (void)> fn ); private: std::array<sf::Keyboard::Key, N> combo; std::function<void (void)> callback; }; .cc template <size_t N> KeyCombinationListener<N>::KeyCombinationListener( const array<sf::Keyboard::Key, N>& sequence, function<void (void)> fn ) : combo(sequence), progress{begin(combo)}, callback{fn} { } In the member initialization of the

How to uniform initialize map of unique_ptr?

陌路散爱 提交于 2019-11-28 07:34:52
问题 I have this code to initialize map from into to unique_ptr. auto a = unique_ptr<A>(new A()); map<int, unique_ptr<A>> m; m[1] = move(a); Can I use uniform initialize this? I tried map<int, unique_ptr<A>> m {{1, unique_ptr<A>(new A())}}; But I got an error. Some part of error message is In instantiation of 'std::_Rb_tree_node<_Val>::_Rb_tree_node(_Args&& ...) [with _Args = {const std::pair<const int, std::unique_ptr<A, std::default_delete<A> > >&}; _Val = std::pair<const int, std::unique_ptr<A>

Copy constructor curly braces initialization

爷,独闯天下 提交于 2019-11-28 03:47:55
问题 "we can initializate objects of a class for which we have not define any constructor using: memberwise initialization. copy initialization. default initialization. For example: struct Work { string author; string name; int year; }; Work s9 { "Bethoven", "Symphony No. 9 in D minor, Op. 125; Choral", 1824 }; // memberwise initialization Work currently_playing {s9}; // copy initialization Work none {}; // default initialization The C++ Programming Language 4th Ed. Chapter 17.3.1 For example:

Why wasn't a double curly braces syntax preferred for constructors taking a std::initializer_list

你离开我真会死。 提交于 2019-11-27 14:00:14
问题 Uniform initialization is an important and useful C++11 feature. However, you can't just use {} everywhere since: std::vector<int> a(10, 0); // 10 elements of value zero std::vector<int> b({10, 0}); // 2 elements of value 10 and 0 respectively std::vector<int> c{10, 0}; // 2 elements of value 10 and 0 respectively std::vector<int> d = {10, 0}; // 2 elements of value 10 and 0 respectively auto e(0); // deduced type is int auto f = 0; // deduced type is int auto g{0}; // deduced type is std:

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

夙愿已清 提交于 2019-11-27 08:13:44
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 perhaps it's a GCC bug? Jesse Good Yes, its a bug . This is something new and was voted in the working

Why doesn't emplace_back() use uniform initialization?

强颜欢笑 提交于 2019-11-27 03:27:12
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/bits/alloc_traits.h:265:4: required from 'static typename std::enable_if<std::allocator_traits<_Alloc>::

How to use C++11 uniform initialization syntax?

落爺英雄遲暮 提交于 2019-11-26 22:53:28
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 = {5}; The compile error for // T t{u}; std::string a; std::string b{a}; Is a combination of four things