initializer-list

Data “member not declared in this scope”

廉价感情. 提交于 2019-12-13 08:02:46
问题 I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member. I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned: ...error: '_bookingVector' was not declared in this scope| I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector): Schedule:

C++ brace initializer list, temporary lifetime

萝らか妹 提交于 2019-12-13 03:44:30
问题 I've got following code: string join(initializer_list<string_view> strings); initializer_list is std::initializer_list and string_view isn't std::string view but very similar class with constructors from const string& and const char*. Then I've got following invocation of join : EXPECT_EQ("this", join({ string("this") })); After small investigation I've found that the first element of resulting initializer list isn't "this" but "\0his" . This is becouse the destructor of the temporary created

Initializer lists and assignment overloading (operator =)

时光总嘲笑我的痴心妄想 提交于 2019-12-12 22:56:46
问题 Does the overloading of the assignment operator propagate to an initializer list? For example, suppose a class: class MyClass { private: std::string m_myString; //std::string overloads operator = public: MyClass(std::string myString); } And a constructor: MyClass::MyClass(std::string myString) : m_myString(myString) { } Will the initializer list work out the assignment operator overload on std::string ? And if not, is there a workaround? Particularly for GCC. 回答1: I think what you are missing

C++ error: no matching function for call to 'print_size'

狂风中的少年 提交于 2019-12-12 18:26:49
问题 I have this code: #include <iostream> #include <vector> template<typename T> void print_size(std::vector<T> a) { std::cout << a.size() << '\n'; } int main() { std::vector<int> v {1, 2, 3}; print_size(v); auto w = {1, 2, 3}; // print_size(w); // error: no matching function for call to 'print_size' // candidate template ignored: could not match 'vector' against 'initializer_list' } ...which compiles and runs without any issues. But if I enable the commented-out line, it produces the error no

using a union-like class in an std::initializer_list

大城市里の小女人 提交于 2019-12-12 04:10:01
问题 In the code below I show union-like class S which contains two non-related structs B and C. I show how to instantiate the non-POD std::string and delete it again and then switch S to S::CC and set the num int. #include <vector> #include <string> #include <iostream> #include <memory> struct B { B() {} ~B() {} std::string str; void Func1() {} }; struct C { C() {} ~C() {} int num; void Func2() {} }; struct S { S() { tag = CC; } S( const S& s ) { switch( s.tag ) { case BB: new ( &b.str ) std:

C++11: Does std::initializer_list store anonymous array? Is it mutable?

天涯浪子 提交于 2019-12-12 03:44:17
问题 Does the C++ standard say that std::initializer_list<T> is a reference to a local anonymous array? If it says, then we should never return such an object. Any section in the standard say so? Another question, are the underlying objects of a std::initializer_list<T> mutable? I tried to modify it: #include <initializer_list> int main() { auto a1={1,2,3}; auto a2=a1;//copy or reference? for(auto& e:a1) ++e;//error for(auto& e:a2) cout<<e; return 0; } But compiled with error : error: increment of

What is the lifetime of the array underlying a std::initializer_list? [duplicate]

扶醉桌前 提交于 2019-12-12 03:09:43
问题 This question already has an answer here : Initializer list of variables (1 answer) Closed 4 years ago . Following this answer https://stackoverflow.com/a/29074955/1098041 I have a curious behavior. In Debug I get the expected output, however in release (I'm using mingw on windows) I get garbage in the test fonction. #include <iostream> #include <initializer_list> template <class T> struct array { T *ptr; size_t len; array() { clear(); } array( T *p, size_t l ) { assign(p,l); } inline void

VC++ 2013 Initializer List issue

走远了吗. 提交于 2019-12-12 02:32:19
问题 I've the following code that doesn't compile with vc++ 2013. Is it a compiler bug? class Test { public: Test() : mTestBuff{ 1, 2, 3, 4 } { } private: const vector< int > mTestBuff; }; error C2661: 'std::vector<int,std::allocator<_Ty>>::vector' : no overloaded function takes 4 arguments This compile fine with GCC 4.8 and MinGW. What can I done to remove the compile error? 回答1: Try this: Test() : mTestBuff({ 1, 2, 3, 4 }) { } Demo here. 来源: https://stackoverflow.com/questions/21533191/vc-2013

How Do I Use a Variable in new[]'s Value Initialization

拜拜、爱过 提交于 2019-12-12 01:54:13
问题 So when new ing an array of char s I can value initialize: const char* foo = new char[4]{'J', 'o', 'n', '\0'}; What I want to know is how to use a variable in that initializer_list : const string initializer{"jon"}; const char* foo = new char[4]{initializer.c_str()}; // This doesn't work, can I make it work? 回答1: You can use a variable, but you can't use a string and expect the compiler to magically split it up. const char* ip = initializer.c_str(); const char* foo = new char[4]{ip[0], ip[1],

moving elements of an initialization_list considered dangerous?

半城伤御伤魂 提交于 2019-12-11 18:36:57
问题 Previously asked questions (1 and 2) on SO seem to suggest that applying std::move on elements of a std::initializer_list may lead to UB. In fact, std::initializer_list iterators prevent effective move because they refer to const elements. The reason is that compilers may use static read-only memory for storing the underlying array of an initializer_list. I think this view is too limiting and I wanna know if experts out here agree. IMO, the following code cannot possibly use the read-only