uniform-initialization

C++ - Uniform initializer with std::string

我的梦境 提交于 2019-12-22 03:57:25
问题 I am trying the uniform intializer with the string class of C++. Below is the code: #include <iostream> #include <string> using namespace std; int main() { string str1 {"aaaaa"}; string str2 {5, 'a'}; string str3 (5, 'a'); cout << "str1: " << str1 << endl; cout << "str2: " << str2 << endl; cout << "str3: " << str3 << endl; return 0; } The output would be: str1: aaaaa str2: a str3: aaaaa This made me scratched my head. Why str2 cannot achieved the desired result as str3 ? 回答1: std::string has

Initialization of const reference member with deleted copy constructor

主宰稳场 提交于 2019-12-21 10:41:30
问题 This code, with a const A& a member in B , where A has a deleted copy constructor, doesn't compile in GCC 4.8.1, but it works OK in clang 3.4: class A { public: A() = default; A(const A&) = delete; A& operator=(const A&) = delete; }; class B{ public: B(const A& a) : a{a} { } private: const A& a; }; int main() { A a{}; B b{a}; } Which one of the compilers is right? The error in GCC is: prog.cpp: In constructor ‘B::B(const A&)’: prog.cpp:11:14: error: use of deleted function ‘A::A(const A&)’ :

What's the difference between `Object obj(args…)` and `Object obj{args…}`?

前提是你 提交于 2019-12-21 04:31:22
问题 The draft book Effective C++11 by Scott Meyers states: Distinguish () and {} when creating objects What's the difference between Object obj(args...) and Object obj{args...} ? and why Scott says so. Update: The question How to use C++11 uniform initialization syntax? asks for HOW, and this question asks for WHY. Update2: I find the following link is helpful and completely answers this question: https://softwareengineering.stackexchange.com/questions/133688/is-c11-uniform-initialization-a

How does the number of braces affect uniform initialization?

不打扰是莪最后的温柔 提交于 2019-12-20 08:40:59
问题 Consider the following code snippet: #include <iostream> struct A { A() {} A(const A&) {} }; struct B { B(const A&) {} }; void f(const A&) { std::cout << "A" << std::endl; } void f(const B&) { std::cout << "B" << std::endl; } int main() { A a; f( {a} ); // A f( {{a}} ); // ambiguous f( {{{a}}} ); // B f({{{{a}}}}); // no matching function } Why does each call fabricate the corresponding output? How does the number of braces affect uniform initialization? And how does brace elision affect all

Why does C++ list initialization also take regular constructors into account?

余生颓废 提交于 2019-12-19 19:02:18
问题 In C++ when using initializer_list syntax to initialize an object, the regular constructors of the object also participate in overload resolution, when no other list initialization rule applies. As far as I understand it, the following code calls X::X(int) class X { int a_; X(int a):a_(a) {} ); void foo() { X bar{3}; } But I don't understand, why regular constructors also are considered in context of initializer_lists. I feel that a lot of programmers now write X{3} to call a constructor

Active member of an union, uniform initialization and constructors

醉酒当歌 提交于 2019-12-19 07:23:27
问题 As the (Working Draft of) C++ Standard says: 9.5.1 [class.union] In a union, at most one of the non-static data members can be active at any time , that is, the value of at most one of the non-static data members can be stored in a union at any time. [...] The size of a union is sufficient to contain the largest of its non-static data members. Each non-static data member is allocated as if it were the sole member of a struct. All non-static data members of a union object have the same address

Aggregate initialization with curly braces

大兔子大兔子 提交于 2019-12-11 05:43:53
问题 In C++11, are the aggregates allowed to be copied with curly-braces syntax? I have the following code: struct s { int x; }; template<class T> struct holder { template<class A> holder(A&& x) : t{x} {} T t; }; Each one of the statements below works. auto s1 = s{1}; auto s2(s1); auto s3{s1}; ///NOTE : this works! However, the second statement below raises the error cannot convert 's' to 'int' in initialization . holder<s> h{5}; holder<s> h1{s{5}}; I am using gcc 4.8.2. Why do I get this error?

How to use std::map::operator= with initializer lists

微笑、不失礼 提交于 2019-12-10 18:48:49
问题 I asked the same question before about boost::assign::map_list_of (which didn't get answered), then I thought maybe using brace initialization would help, but it didn't. This works perfectly: std::map<int, char> m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}}; But this doesn't: std::map<int, char> m; m = {{1, 'a'}, {3, 'b'}, {5, 'c'}, {7, 'd'}}; Visual Studio 2013 gives the error error C2593: 'operator =' is ambiguous , could be either operator=(std::initalizer_list) or operator=(std::map&&) .

Should I use () or {} when forwarding arguments?

 ̄綄美尐妖づ 提交于 2019-12-10 12:31:33
问题 I have the following class: struct foo { std::size_t _size; int* data; public: explicit foo(std::size_t s) : _size(s) { } foo(std::size_t s, int v) : _size(s) { data = new int[_size]; std::fill(&data[0], &data[0] + _size, v); } foo(std::initializer_list<int> d) : _size(d.size()) { data = new int[_size]; std::copy(d.begin(), d.end(), &data[0]); } ~foo() { delete[] data; } std::size_t size() const { return _size; } }; And I want to forward arguments to it like this: template <typename... Args>

Unneeded conversion happening on std::move

不打扰是莪最后的温柔 提交于 2019-12-10 11:44:00
问题 Why is the compiler thinking I am trying to convert from an object of type Container to an object of type MoveTest in the following code? Why is the braced constructor being forwarded to the move_things variable? Replacing the {} with () of course solves the problem #include <iostream> #include <array> using namespace std; static constexpr int NUMBER{2}; class MoveTest { public: MoveTest(const MoveTest&) { cout << "MoveTest(const MoveTest&)" << endl; } MoveTest(MoveTest&&) { cout << "MoveTest