initializer-list

Overloaded parent class constructors. Wrong inizializer choice?

别来无恙 提交于 2019-12-11 14:56:46
问题 I would like to add a Child class to a pre-existing project having Parent already defined and declared. Parent class has got two constructors with initializer-list. This is my code, and it generates error C2668: 'Parent::Parent' : ambiguous call to overloaded function. Where is my error? Thanks @Mape for your snippet #include <stdio.h> class Parent { public: // Here a constructor with one default trailing argument, i.e. myInt, // myDouble is not initialised. This is correct, as once one

Using auto with initializer list

邮差的信 提交于 2019-12-11 13:54:26
问题 I have question regarding interaction between auto and initializer list. Example code: #include <iostream> int main() { auto a{ 1 }; auto b = { 1 }; auto c = 1; std::cout << typeid(a).name() << std::endl; std::cout << typeid(b).name() << std::endl; std::cout << typeid(c).name() << std::endl; return 0; } Gives output: int class std::initializer_list<int> int Which is kind of confusing. I'm posting this question as a followup to this. What should happen? I've did some research and it seems that

Calling constructor with braces instead parantheses

给你一囗甜甜゛ 提交于 2019-12-11 10:49:54
问题 I recently realized that in C++11 we can call a delegating initializer-list constructor like Foo() : Foo{42} // delegate to Foo(initializer_list<>) Is this syntax correct? It seems to be, although I would have expected to always use parentheses when calling a function, like Foo({42}) . The example code below compiles fine in both clang++ and g++ #include <iostream> #include <initializer_list> struct Foo { Foo() : Foo{42} // I would have expected invalid syntax, use Foo({42}) { std::cout <<

C++ : initialize(new) an array of vector of different initial size

99封情书 提交于 2019-12-11 09:09:11
问题 To help you get the point, I give my codes:(main.cpp),only one file involved. #include <iostream> #include <vector> using namespace std; class test{ public : int member {0}; void fun(){cout << "member is " << member << endl;} test(){} //test(int param) : member(param){} //this line is commented. }; int main() { vector<test> *vp = new vector<test>[2] {{10},{20}}; //vector<test> array[2] {{10},{20}};//this won't work either. cout << "size of vp[0] is " << vp[0].size() << endl; cout << "size of

How to construct std::array object with initializer list? [duplicate]

你说的曾经没有我的故事 提交于 2019-12-11 05:05:04
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How do I initialize a member array with an initializer_list? You can construct an std::array just fine with an initializer list: std::array<int, 3> a = {1, 2, 3}; // works fine However, when I try to construct it from an std::initializer_list as a data member or base object in a class, it doesn't work: #include <array> #include <initializer_list> template <typename T, std::size_t size, typename EnumT> struct

std::initializer_list and order of evaluation of the elements [duplicate]

风格不统一 提交于 2019-12-11 02:08:13
问题 This question already has answers here : Are multiple mutations within initializer lists undefined behavior? (2 answers) Closed 6 years ago . Is the comma ( , ) a sequence point in std::initializer_list ? example: is this UB or not: #include <vector> int main() { auto nums = [] { static unsigned x = 2; return ( x++ % 2 ) + 1; }; std::vector< int > v{ nums(), nums(), nums(), nums(), nums() }; // not sure if this is different: (note the additional brackets) // std::vector< int > v({ nums(),

Differences in g++ STL container initializer_list behavior betweeen 4.4 and 4.7

老子叫甜甜 提交于 2019-12-11 00:57:19
问题 The following c++0x/c++11 feature (initializer lists, including for STL containers) should allow me to initialize this STL std::map at declaration. And, it works just fine in g++ 4.7.2, but I get an error trying to compile it in g++ 4.4.6. According to the GCC docs, this c++0x feature was available in 4.4, but clearly either (a) I'm doing something wrong, or (b) it was incomplete in g++ 4.4. std::map<std::string,std::vector<std::string>> DbConnection::attrs_of_type = { { "http", { "url",

Overloading >>operator and initiatializing using an initializer list of custom type

时光毁灭记忆、已成空白 提交于 2019-12-10 20:09:01
问题 I've been trying to make a very simple map container and I thought it would be nice to be able to initialize it like so: Hash table = { "name1" >> value, "name2" >> value2, "name3" >> value3, "name4" >> value4 }; How I was going to go about this was by first making a pair(duo) data structure which would keep the name and the value of each element, overload the >> operator to return a duo using the name and value parameters and make a constructor for the hash class to initialize it by using

C++ initializer list capabilities: call functions without initializing member?

十年热恋 提交于 2019-12-10 19:07:49
问题 This is a question on the syntax of C++ initializer lists. Is it possible to call functions from initializer lists without them being arguments to member object constructors? Code example listed below is paraphrased (paracoded?) from a similar situation at work. The Situation A member variable takes a pointer to a singleton as constructor argument. The member variable is constructed by initializer list in its containing class' constructor. The singleton has not been created prior to the

In-class member initializer fails with VS 2013

醉酒当歌 提交于 2019-12-10 18:26:19
问题 I expected the following code to compile, but Visual Studio 2013 Update 2 gives me an error, while g++ 4.7 compiles it fine. using std::vector; using std::string; struct Settings { vector<string> allowable = { "-t", "--type", "-v", "--verbosity" }; }; VS 2013 compile fails with: 'std::vector<std::string,std::allocator<_Ty>>::vector' : no overloaded function takes 4 arguments If I change the member as follows then it compiles fine: vector<string> allowable = vector<string> { "-t", "--type", "