list-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

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

别来无恙 提交于 2019-12-21 03:52:44
问题 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

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

Initializing scalars with braces

我们两清 提交于 2019-12-19 12:53:14
问题 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. 回答1: What is the rationale behind allowing the initialization

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

杀马特。学长 韩版系。学妹 提交于 2019-12-18 18:53:28
问题 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? 回答1: 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

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

只谈情不闲聊 提交于 2019-12-17 08:26:36
问题 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

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

こ雲淡風輕ζ 提交于 2019-12-17 08:26:12
问题 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

Braced initialisation of std::map member compiler error

爷,独闯天下 提交于 2019-12-14 02:32:32
问题 The following code does not compile using Visual Studio 2013. It does compile using Xcode 6.1 (Clang 3.5). std::string s1("one"); std::string s2("two"); std::string s3("three"); std::string s4("four"); class X { typedef std::map<std::string, std::string> MyMapType; MyMapType map1 = { { s1, s2 }, { s3, s4 } }; MyMapType map2 = { { std::make_pair(s1, s2) }, { std::make_pair(s3, s4) } }; }; The error reported for both declarations is: error C2664: 'std::map<std::string,std::string,std::less<_Kty

Brace-enclosed initializer list of templated struct

自古美人都是妖i 提交于 2019-12-12 17:18:59
问题 #include <array> #include <vector> #include <cinttypes> #include <iostream> using namespace std; template<size_t N> struct item_t { array<uint32_t, N> weight = {0}; }; int main(void) { vector<item_t<3>> items; items.emplace_back({{9,2,3}}); cout << items[0].weight[0] << endl; return 0; }; I'm at a bit of a loss here. Error is on the emplace_back line and no idea how to resolve it. Any help or hints would be appreciated, thanks. EDIT gcc version 4.8.2 $ g++ -std=c++11 test.cpp test.cpp: In

C++ list initialization allows multiple user-defined conversions

本小妞迷上赌 提交于 2019-12-12 10:38:59
问题 I was reading this answer, which has the following example: struct R {}; struct S { S(R); }; struct T { T(const T &); //1 T(S); //2 }; void f(T); void g(R r) { f({r}); } The answer is related to an old version of [over.best.ics]/4, which back then looked like this: However, when considering the argument of a constructor or user-defined conversion function that is a candidate by [over.match.ctor] when invoked for the copying/moving of the temporary in the second step of a class copy