FAQ Database Discussion Community


Enable default initializer list constructor

c++,c++11,inheritance,initializer-list,list-initialization
I believe modern C++ initializer lists are very useful for initializing objects, to the point of removing the need for defining your own constructor: struct point { float coord[3]; }; point p = {1.f, 2.f, 3.f}; // nice ! However, this doesn't work when my class inherits from another class:...

Using derived class members for the initialization of the parent class

c++,initialization,derived-class,initializer-list
I would like to initialize a class B that's derived from class A, and where in B I construct a cache first that is used with the construction of A, e.g., class B: public A { public: B(): A(cache_), cache_(this->buildCache_()) protected: const SomeDataType cache_; private: SomeDataType buildCache_() const { //...

Nested initializer list c++

c++,constructor,initializer-list
I'm working on a following class representing the 15-puzzle (http://en.wikipedia.org/wiki/15_puzzle) : class fifteen { private: static constexpr size_t dimension = 4; using position = std::pair< size_t, size_t > ; public: size_t table [ dimension ][ dimension ]; size_t open_i; size_t open_j; public: fifteen( ); fifteen( std::initializer_list< std::initializer_list< size_t >> init...

Initialization of const reference member in initializer list

c++,gcc,reference,initialization,initializer-list
I was playing with some useless code to understand initialization of member references, and bumped into this: struct A {}; struct B { B() : a() { } const A& a; }; The code above gives the following error when compiled with gcc 4.9.2: In constructor 'B::B()': error: value-initialization of...

C++1z handling of a == test with std::initializer_list with and w/o const in an auto function

c++,auto,c++14,initializer-list,c++1z
c_NEXT is defined in class AskBase: struct Status { static auto constexpr c_ROOT {-999}; static auto constexpr c_PREVIOUS {-1}; static auto constexpr c_NEXT {1}; static auto constexpr c_EOF {999}; }; Later c_NEXT is used in template<typename Container> class AskUI auto run(){ auto status = AskBase::Status::c_NEXT; for (typename Container::iterator ii=m_asks.begin(); ii!=m_asks.end();...

C++11 vector with two GLfloats as pair fail to uniform initialize

c++,c++11,stdvector,initializer-list,std-pair
The error message of gcc 4.9.2 is: could not convert from '<brace-enclosed initializer list>' to 'std::vector<std::pair<float, float> >' of this code: vector<pair<GLfloat, GLfloat>> LightOneColorsPairVec {{0.5f, 0.5f, 0.5f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f}}; The code is compiled with 'std=c++11' compiler flag....

std::make_map? uniform initialization

c++,initializer-list,stdmap,uniform-initialization,stdtuple
The following works: std::map<int,int> m{{1,2}}; std::tuple<std::map<int,int>,int> tm1{m, 2}; std::tuple<std::map<int,int>,int> tm2{{{1,2}}, 2}; std::tuple<std::map<int,int>,int> tmm1 = std::make_tuple(m, 2); The following does not: std::tuple<std::map<int,int>,int> tmm2 = std::make_tuple({{1,2}}, 2); Is there a way to initialize a map inside a tuple made with make_tuple without using an extra variable?...

initializer_list and argument-dependent lookup

c++,c++11,initializer-list,argument-dependent-lookup
I'm trying to use an std::initializer_list as an argument in a function that uses argument-dependent lookup (ADL). But I don't get it to work and I don't understand why. The following is a minimal failing example: #include <initializer_list> #include <iostream> class Foo { public: inline friend void bar(std::initializer_list<Foo> v) {...

Can't I return a list initialization directly in C++?

c++,initializer-list
struct vec2{float x,y;}; This will work: vec2* window(float h, float w) { vec2 ret[4] = {{w,h},{1-w,h},{1-w,1-h},{w,1-h}}; return ret; } But this won't: vec2* window(float h, float w) { return {{w,h},{1-w,h},{1-w,1-h},{w,1-h}}; } I guess the error comes from the fact that the latter doesn't specify the size of the array, but...

How do I pass a list of values to a function expecting an array?

c++,arrays,visual-studio-2012,initializer-list
In my program I want to pass a few variables into a function and have that function run a for loop to write the data to console. This is my code: void WriteValue(int[] arr) { for(auto c : arr) std::cout<<arr<<std::endl; } int main() { int a = 0; int b...

Initializer list of variables

c++,c++11,initializer-list
Is it possible to create an initializer_list of variables, like function arguments for example (cf. function test)? The code below works, and neither clang nor gcc complain about anything, but I would just like to make sure this is ok. edit: changing the argument of wrap to a const ref...

initializer list constructor error with CRTP

c++,c++11,initializer-list,crtp
I'm wetting my feet with C++11 and am really confused why this doesn't work: template <class T> struct A { size_t size() const { return sizeof(T); } }; struct B : A<B> { int x; int y; }; B var {1, 5}; I'm using gcc 4.8.2 and get an error...

How to design classes with constructor taking a std::initializer_list?

c++,c++11,vector,initializer-list
When classes have a constructor overload taking a std::initializer_list, this overload will take precedence even if other constructor overloads are seemingly a better match. This problem is described in detail in Sutter's GotW#1, part 2, as well as Meyers' Effective Modern C++, Item 7. The classic example of where this...

forwarding initializer list expressions

c++,templates,c++11,initializer-list
Initializer list expressions are really convenient for initializing C++ containers: std::vector<int>({1,2,3}) ...but it seems that a brace-enclosed initializer list expression, like {1,2,3} will only bind to a function that takes a std::initializer_list<int> - it doesn't seem to bind to a universal (forwarding) reference: template <class T> void foo(T&& v) {...

Is this code valid or not? GCC and Clang disagree

c++11,gcc,clang,initializer-list
The following code gives me different results on GCC and Clang. Who's right? union Foo { struct { int a; int b; }; }; struct Bar { Bar(void) : test{.a = 1, .b = 2} { } Foo test; }; I get the following error with GCC (and it compiles...

How to construct a vector with unique pointers

c++,c++11,unique-ptr,initializer-list,list-initialization
I try to construct a vector with unique_ptr. But I do not find a direct way. The following code does not compiles. The error is:Call to implicitly-deleted copy constructor of 'std::__1::unique_ptr >': #include <iostream> #include <memory> #include <utility> #include <vector> class test1{ public: test1(){}; test1(test1&&)=default; }; int main(int argc, const...

Implicit use of initializer_list

c++,c++11,initializer-list
§[dcl.init.list] 8.5.4/2: The template std::initializer_list is not predefined; if the header <initializer_list> is not included prior to a use of std::initializer_list — even an implicit use in which the type is not named (7.1.6.4) — the program is ill-formed. Does that mean this program is ill-formed? #include <vector> int main()...

how to assign an array from an initializer list

c++,arrays,initializer-list
I have a limited knowledge about c++. I tried to compile a c++ library and when I run the make file for the following header file mcmc_dhs.h #include <algorithm> #include <map> // intrinsic shape and (reduced) shear just add? //#define WLNOISE // use shear instead of reduced shear for model...

What is the Go equivalent of C++ initializer lists?

go,initializer-list
In C++, I might do the following: for (const string& key : {"foo", "bar", "baz"}) { DoSomeThingWithKey(key); } The {"foo", "bar", "baz"} is a std:initializer_list. Awesomeness. Is there an equivalent idiomatic pattern for Go?...

Strange values in a lambda returning initializer_list

c++,c++11,lambda,initializer-list
Consider this C++11 code snippet: #include <iostream> #include <set> #include <stdexcept> #include <initializer_list> int main(int argc, char ** argv) { enum Switch { Switch_1, Switch_2, Switch_3, Switch_XXXX, }; int foo_1 = 1; int foo_2 = 2; int foo_3 = 3; int foo_4 = 4; int foo_5 = 5; int foo_6...

no appropriate default constructor available error with initializer list

c++,visual-c++,inheritance,constructor,initializer-list
For some reason I'm getting the no default constructor error even though I'm using a member initializer. What am I doing wrong? A minimal example, a.cpp #include "a.h" a::a(int x, int y, int z):x(x),y(y),z(z) { } a.h class a { public: a(int x, int y, int z); private: int x,...