Menu
  • HOME
  • TAGS

Why are initializer lists not available when changing the allocator of std::vector?

c++,c++11,memory-alignment,initializer-list,eigen3

After studying the Eigen/StdVector include file (to be precise it is in Eigen/src/StlSupport/StdVector.h line 68 of version 3.2.1), it seems that the problem originates from a partial template specialization of the std::vector within this header file. This partial template specialization replaces the STL vector as soon as you use Eigen::aligned_allocator...

Efficient object construction supporting empty initializer lists

c++,c++11,initializer-list,perfect-forwarding

It's not clear why adding a default constructor isn't an option for you, allowing simply S s{}: struct S { S() = default; S( S&& s ) = default; std::vector< int > member; }; If that isn't suitable you have a number of options. The most obvious is to get...

Is it possible to initialize a vector in C++ like in Matlab?

c++,matlab,vector,operator-overloading,initializer-list

Just use std::vector. If you must write it yourself with arrays, probably the easiest way is to first write an initializer list constructor as described in the question you linked to: Vec(unsigned size) : N(size), e(new double[size]) {} Vec(std::initializer_list<double> l) : Vec(l.size()) { std::copy(l.begin(), l.end(), e); } Then write a...

Initializing fixed number of variables with an initializer list

c++,c++11,initializer-list

the best I could come up with when using std::initialiser_list struct Pixel { Pixel(std::initializer_list<uint8_t> rgba) : _rgba { rgba } { switch(_rgba.size()) { case 0: _rgba.push_back(0); case 1: _rgba.push_back(0); case 2: _rgba.push_back(0); case 3: _rgba.push_back(0); case 4: break; default: throw std::invalid_argument { "" }; } } std::vector<uint8_t> _rgba; }; ......

Why is initializer_list constructor preferred here?

c++,c++11,constructor,language-lawyer,initializer-list

§13.3.1.7 [over.match.list]/p1: When objects of non-aggregate class type T are list-initialized (8.5.4), overload resolution selects the constructor in two phases: Initially, the candidate functions are the initializer-list constructors (8.5.4) of the class T and the argument list consists of the initializer list as a single argument. If no viable initializer-list...

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

With the adoption of N3922 at the November 2014 committee meeting, the four initializations in the question will now deduce int rather than initializer_list<int>. The paper makes auto used in the direct-list-initialization context (i.e., auto x{/*...*/};) ill-formed unless the list contains only one element, in which case the type is...

forwarding initializer list expressions

c++,templates,c++11,initializer-list

It's not that it can't bind to the parameter of your function it's just that the compiler is unable to detect the type of your template, this compiles: #include <vector> template <class T> void foo(T&& v) { std::vector<int>(std::forward<T>(v)); } int main() { foo(std::initializer_list<int>{1,2,3}); } ...

In C++ template copy assignment operator not compatible with initializer_list?

c++,templates,c++11,initializer-list

Because initializer list is a non-deduced context. From [temp.deduct.type]: The non-deduced contexts are: — [...] — A function parameter for which the associated argument is an initializer list (8.5.4) but the parameter does not have a type for which deduction from an initializer list is specified (14.8.2.1). [ Example: template<class...

Is this code valid or not? GCC and Clang disagree

c++11,gcc,clang,initializer-list

Using GCC 4.9.1 and the following options: -Wall -Wextra -std=c++11 -pedantic this is what you get: prog.cc:7:5: warning: ISO C++ prohibits anonymous structs [-Wpedantic] }; ^ prog.cc: In constructor 'Bar::Bar()': prog.cc:11:21: warning: ISO C++ does not allow C99 designated initializers [-Wpedantic] Bar(void) : test{.a = 1, .b = 2} {...

std::initializer_list and reference types

c++,c++11,initializer-list

Can a std::initializer_list contain reference types (both rvalue and lvalue)? std::initializer_list<T> doesn't hold references to its elements. It uses copy-semantics by holding its values as const objects: 18.9 Initializer List [support.initlist] An object of type initializer_list<E> provides access to an array of objects of type const E. An initializer_list...

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

c++,c++11,stdvector,initializer-list,std-pair

First of all because std::pair doesn't have constructor that takes a std::initializer_list. Secondly because std::pair is a pair, it only have two values, not four.

initializer_list and argument-dependent lookup

c++,c++11,initializer-list,argument-dependent-lookup

I believe that the problem is that the subexpression1 { a, a } does not really have a type, and as such it does not have associated types or namespaces which in turn means that ADL does not kick in. If you have a function in the global namespace, normal...

C++ get initializer list for constructor with other parameters

c++,initializer-list

You can change your constructor to just get the initializer_list with: class V { private: std::vector<int> m_list; int m_size; public: V(std::initializer_list<int> init_list): m_list(init_list.begin(), init_list.end()), m_size(m_list.size()) { } }; and construct V objects with: V v1 = {1,2}; V v2({1, 2}); You don't need the size to be input anymore thanks...

Modeling Initializer Lists in C++03

c++,templates,initializer-list,c++03

It seems clunky and not very user-friendly. Is it possible to reduce this to a single line in C++03? Since you are basically imitating std::array, just rid of the constructor: template <typename T, int N> class N_Tuple { public: T values_[N]; }; Note, in this, you have to make...

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

c++,initializer-list

You can't return an array by value, and you mustn't return a pointer to an automatic or temporary object, since it will be destroyed when the function returns, leaving the pointer invalid. Return std::array if the size is a known, small constant or std::vector otherwise....

initializer_list immutable nature leads to excessive copying

c++,c++11,move-semantics,c++14,initializer-list

There is a recent proposal for movable initializer lists, where, in particular, the authors say: std::initializer_list was designed around 2005 (N1890) to 2007 (N2215), before move semantics matured, around 2009. At the time, it was not anticipated that copy semantics would be insufficient or even suboptimal for common value-like classes....

Cannot convert from 'initializer-list' to 'std::initializer_list &'

c++,visual-studio-2013,initializer-list

You are trying to bind a temporary to a non-const reference; std::initializer_list<Keyword>& lis Try either; std::initializer_list<Keyword> const& lis Or std::initializer_list<Keyword> lis When building with GCC, enable -Wall -pedantic it should give you an error then as well....

Strange values in a lambda returning initializer_list

c++,c++11,lambda,initializer-list

From: http://en.cppreference.com/w/cpp/utility/initializer_list The underlying array is not guaranteed to exist after the lifetime of the original initializer list object has ended. The storage for std::initializer_list is unspecified (i.e. it could be automatic, temporary, or static read-only memory, depending on the situation). I don't think the initializer list is copy-constructable. std::set...

Typecast initializer_list

c++,initializer-list

No, the conversion you are trying to perform cannot be accomplished with a cast---at least, not safely. In addition, there are only three ways to create a new std::initializer_list object: With the default constructor: std::initializer_list<unsigned char> IL; With the braced-init-list syntax: std::set<int> {1, 2, 3}; By copying another initializer list...

String assignment with initializer lists

c++,c++11,initializer-list

string s {"IDE"}; // Type of s is explicit - std::string auto S{"IDE"}; // Type of S is an initializer list consisting of one char const*. auto c = {"IDE"}; // Type of c is same as above. auto C {string{"IDE"}}; // Type of C is an initializer list consisting...

Why must initializer list order match member declaration order?

c++,constructor,initializer-list

The warning is trying to prevent situations where you might be relying on the wrong ordering of the data members. Say you think B is initialized before A, and then you do something like this: myClass::myClass() : B(42), A(B) {} Here, you have undefined behaviour because you are reading from...

how to assign an array from an initializer list

c++,arrays,initializer-list

You cannot assign directly to an array after its declaration. Basically your code is the same as int main() { double arr[2][2]; arr = { {1, 2}, {3, 4.5} }; // error } You have to either assign the value at declaration double arr[2][2] = { {1, 2}, {3, 4.5}...

Assignment of initializer list

c++,c++11,operator-overloading,initializer-list,overload-resolution

Unlike auto, where a braced-init-list is deduced as an initializer_list, template argument deduction considers it to be a non-deduced context, unless there exists a corresponding parameter of type initializer_list<T>, in which case the T can be deduced. From §14.8.2.1/1 [temp.deduct.call] (emphasis added) Template argument deduction is done by comparing each...

Why scalar in braces are not interpeted as initializer_list

c++,language-lawyer,initializer-list

First, something very important: You have two different kinds of constructors. The first in particular, C(std::initializer_list<int>), is called an initializer-list constructor. The second is just a normal user-defined constructor. [dcl.init.list]/p2 A constructor is an initializer-list constructor if its first parameter is of type std::initializer_list<E> or reference to possibly cv-qualified std::initializer_list<E>...

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

go,initializer-list

Simply use a slice: for _, value := range []string{"foo", "bar", "baz"} { fmt.Println(value) } or alternatively an array: for _, value := range [...]string{"foo", "bar", "baz"} { fmt.Println(value) } ...

lambda return in initializer list

c++,c++11,lambda,initializer-list

Performance-wise, it should not matter all that much. You don't copy around any Bitmap objects, and the construction of your lambda should not take any noticeable time. But for readability, I would create a static member function instead of a lambda here: class RuleNameConverter { public: RuleNameConverter(const boost::property_tree::ptree& pt); private:...

Merge vector and initializer_list in initialization of vector>?

c++,c++11,vector,initializer-list

One possible solution is to use a helper function that does the appending. vector<string> appendStrings(vector<string>&& s1, vector<string> const& s2) { s1.insert(s1.end(), s2.begin(), s2.end()); return s1; } And use it to initialize the variable. vector<string> suffix {{"1", "2", "3"}}; vector<vector<string>> v = {{ appendStrings({"a", "b", "c"}, suffix), {"aa", "bb"}, appendStrings({"xyz", "yzx",...

Initialization of const reference member in initializer list

c++,gcc,reference,initialization,initializer-list

GCC is correct in its interpretation of {}. [dcl.init.list]/p3.8-9 (quoting N4296; earlier drafts has the same relative ordering of these two bullets): List-initialization of an object or reference of type T is defined as follows: [7 inapplicable bullets omitted] Otherwise, if T is a reference type, a prvalue temporary of...

c2797 List initialization inside member initializer list or non static data member initializer not implemented

c++,visual-studio,c++11,initializer-list

Visual Studio has no implement this feature yes. Workaround can be found here You can just use const ProcessingOrder m_ProcessingOrder = ProcessingOrder { Process::TUNNEL_IP_VERSION, Process::PADDING_BYTE, Process::IP_ADDRESS_FIT_IPv6_SIZE, Process::PORT_NUMBER }; For your second case. struct Attribute_t{ SSL_CTX* const m_pContext; Socket* m_pSocket; X509* m_pCertificate; }m_Attribute; then just m_Attribute = Attribute_t{SSL_CTX_new(g_SSLChoice[version].m_pfSSLMethod()), 0, 0} ...

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

c++,c++11,vector,initializer-list

I would take the same approach that the standard took with piecewise_construct in pair or defer_lock in unique_lock: using tags on the constructor: struct n_copies_of_t { }; constexpr n_copies_of_t n_copies_of{}; template <typename T, typename A = std::allocator<T>> class vector { public: vector(std::initializer_list<T>); vector(n_copies_of_t, size_type, const T& = T(), const A&...

Using double braces e.g. Foo{{initializer list}} to resolve ambiguity

c++,c++11,initializer-list

In the end I solved this problem by having my class use a variadic parameter pack rather than an initializer list. I haven't yet come across an example where you can't bypass initializer lists in this way....

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

c++,variables,c++11,new-operator,initializer-list

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], ip[2], ip[3]}; Once you have a variable length, though, this doesn't work at all. You just have to...

In-class member initialization with an initializer list using uniform initialization syntax? [duplicate]

c++,c++11,initializer-list,uniform-initialization,in-class-initialization

Here is the responsible grammar from N3797: // after a member declaration: braced-or-equal-initializer-list: = initializer-clause braced-init-list braced-init-list: { initializer-list ,OPT } { } initializer-list: initializer-clause initializer-list, initializer-clause initializer-clause: assignment-expression braced-init-list So I'd say the first statement is correct and it is indeed accepted by a recent gcc and clang....

Inconsistent gcc diagnostic for string initialization

c,gcc,initializer-list

While: char a[5] = {'h','e','l','l','o','\0'}; is invalid. (C11, 6.7.9p2) "No initializer shall attempt to provide a value for an object not contained within the entity being initialized." This: char b[5] = "hello"; is explicitly allowed by C (emphasis mine): (C11, 6.7.9p14) "An array of character type may be initialized by...

std::make_map? uniform initialization

c++,initializer-list,stdmap,uniform-initialization,stdtuple

No, you cannot just call make_tuple, since first arg in this case will be initializer_list. Specify manually. std::tuple<std::map<int,int>,int> tmm2 = std::make_tuple<std::map<int, int>>({{1,2}}, 2); Create temporary map from init list. std::tuple<std::map<int,int>,int> tmm2 = std::make_tuple(std::map<int, int>{{1,2}}, 2); ...

initializer list constructor error with CRTP

c++,c++11,initializer-list,crtp

Aggregate-initialization requires your type to be an aggregate. An aggregate cannot have base classes: An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3). ...

Visual Studio std::array initializer bug (2797)

c++,initializer-list,stdarray

In this particular case you can just add a pair of parentheses: vec(Args&&... args) : v({{args...}}) {} This works with VS2013 which, I suppose, you are using. With VS2015 the code works without modifications. Also note that for C++ conformance vec2 should be rewritten as vec2(T x, T y) :...

Using derived class members for the initialization of the parent class

c++,initialization,derived-class,initializer-list

What you are doing as-is is undefined behavior, from [class.base.init]/14, emphasis mine: Member functions (including virtual member functions, 10.3) can be called for an object under construction. Similarly, an object under construction can be the operand of the typeid operator (5.2.8) or of a dynamic_cast (5.2.7). However, if these operations...

Implicit use of initializer_list

c++,c++11,initializer-list

Your program is not ill-formed because <vector> is guaranteed to include <initializer_list> (the same is true for all standard library containers) §23.3.1 [sequences.general] Header <vector> synopsis #include <initializer_list> ... Searching the standard for #include <initializer_list> reveals the header is included along with the following headers <utility> <string> <array> <deque> <forward_list>...

How to construct a vector with unique pointers

c++,c++11,unique-ptr,initializer-list,list-initialization

This make_vector is a function that takes any number of arguments, and perfect-forwards them into a vector. // get the first type in a pack, if it exists: template<class...Ts> struct first {}; template<class T, class...Ts> struct first<T,Ts...>{ using type=T; }; template<class...Ts> using first_t=typename first<Ts...>::type; // build the return type: template<class...

Why does {1, 2} has no a type, but decltype(a) is well-formed?

c++,initializer-list

decltype({1, 2}) is illegal, however, the type of a has already been deduced. From the closest draft to the C++11 standard, N3337: §7.1.6.4/6 Once the type of a declarator-id has been determined according to 8.3, the type of the declared variable using the declarator-id is determined from the type of...

no appropriate default constructor available error with initializer list

c++,visual-c++,inheritance,constructor,initializer-list

Your b has two a objects in it: one is called ao and is a member variable, and the other is the one b is inherited from. You're already initializing ao explicitly in the initializer list, but you're not initializing b's parent. You can do this by inserting a(4,5,6), in...

Does initialization list work for base classes?

c++,initializer-list

Section § 8.5.1 of the standard defines an aggregate : An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions...

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

c++,arrays,visual-studio-2012,initializer-list

Here's a very simple and flexible way: #include <iostream> template<typename T> void WriteValue(const T& arr) { for(auto c : arr) std::cout << c << std::endl; } int main() { int a = 0; int b = 1; int c = 3; WriteValue(std::array<int, 3>{a,b,c}); // nicer C99 way: WriteValue((int[]){a,b,c}); return 0;...

Initializer list of variables

c++,c++11,initializer-list

auto ar = wrap({a,b,c}); This create a temporary array of type int[3], then binds an initializer_list<int> to that array, then calls wrap which creates an array<const int> that refers to the array. At the end of the expression the array is destroyed, leaving the array<const int> with a dangling...

Nested initializer list c++

c++,constructor,initializer-list

I managed to do it with the initializer lists, here's how if anyone's interested: fifteen( std::initializer_list< std::initializer_list< size_t >> init) { int pos_i = 0; int pos_j = 0; for(auto i = init.begin(); i != init.end(); ++i ) { for(auto j = i->begin(); j!= i->end(); ++j) { table [pos_i][pos_j] =...

C++11 initializer list fails - but only on lists of length 2

c++,c++11,initializer-list

Introduction Imagine the following declaration, and usage: struct A { A (std::initializer_list<std::string>); }; A {{"a" }}; // (A), initialization of 1 string A {{"a", "b" }}; // (B), initialization of 1 string << !! A {{"a", "b", "c"}}; // (C), initialization of 3 strings In (A) and (C), each c-style...

Simple example of an initializer list

c++,c++11,initializer-list

In this case, a simple constructor will work: foo(int x, int y) : x(x), y(y) {} If the class were an even simpler aggregate (which yours would be if the data members were public) then you wouldn't even need that - this style of initialisation will initialise each member of...

Check whether an element is in std::initializer_list

c++,c++11,boost,initializer-list

Since a std::set's count only returns 1 or 0 you could just use that: if(set<int>{4, 8, 15, 16, 23, 42}.count(x)) Keep in mind that bare numbers can be confusing for your audience (and cause 5 seasons of Lost.) I'd recommend declaring your numbers as const and giving them a meaningful...

Why isn't `std::initializer_list` defined as a literal type?

c++,language-lawyer,c++14,initializer-list,constexpr

The standard committee seems to intend on initializer_list being a literal type. However, it doesn't look like it's an explicit requirement, and seems to be a bug in the standard. From § 3.9.10.5: A type is a literal type if it is: - a class type (Clause 9) that has...

Enable default initializer list constructor

c++,c++11,inheritance,initializer-list,list-initialization

Your original case relied upon aggregate initialization [dcl.init.list]: List-initialization of an object or reference of type T is defined as follows: ... — Otherwise, if T is an aggregate, aggregate initialization is performed Where an aggregate and aggregate initialiazation are, from [dcl.init.aggr], emphasis mine: An aggregate is an array or...

How to make initializer list implicitly convert to the class?

c++,initializer-list

The call of the function with the initializer list will work. You should do nothing special.:) The call would not be compiled if the constructor had function specifier explicit. In this case you have to use the previous call of the function f( A{1, 3.14} ); using functional notation of...

Nested hash_map initilizer lists

c++,hashmap,visual-studio-2013,initializer-list

There are numerous (well, at least 3) intrusive bugs related to initializer lists and uniform initialization in MSVC2013. Update According to the comments, this particular bug was removed in VS13 Update 2. Sadly, the advice is to... stay away from many of them for now. I keep the following rule...

initializer list vs. vector

c++,c++11,vector,initialization,initializer-list

The common use of std::initializer_list is as argument to constructors of container (and similar) classes, allowing convenient initialisation of those containers from a few objects of the same type. Of course, you can use std::initializer_list otherwise and then use the same {} syntax. Since a std::initializer_list has a fixed size,...

convert 'std::initializer_list' to 'int'

c++,c++11,initializer-list

std::initializer_list<T> cannot convert to T for the same reason that T[] cannot convert to T, and vector<T> cannot convert to T: if you merely know that you have a std::initializer_list<T>, you don't know how many elements it has. std::initializer_list<int> x = { 1, 2, 3 }; is perfectly valid. If...

C++. Inheritance and initializing member variable has compile error

c++,inheritance,initialization,initializer-list

In SubClass(int superArg, int subArg) : subVar(subArg) { SuperClass(superArg); } compilers recognize SuperClass(superArg); as a declaration (equivalent to SuperClass superArg;), (whereas I expected a creation of a temporary SuperClass). So you have superArg declared once with int once with SuperClass. A minimal code to expose your problem: void foo(int arg)...