initializer-list Questions

2

In the following if I use constexpr the compiler apparently the compiler says "expression must have a constant value", this happens on MSVC and GCC: int main() { constexpr auto nnn = { ...
Puberulent asked 19/9 at 7:56

1

Consider the following example that compiles with clang but is rejected by edg, gcc and msvc. Demo #include <initializer_list> struct C { C(){} C(std::initializer_list<int> i = {3})...

1

Solved

I learned that when a class has a user-provided constructor then the compiler will not implicitly generate a default constructor. I wrote the following code, C d{}; in particular, and I expect it t...
Eyeopener asked 5/8 at 20:0

3

I have a bunch of longish hand initialized arrays, e.g.: const std::array<int, 16> a{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; const std::array<int, 16> b{16,17,18,19,20,21,22,23,24,25,26...
Easiness asked 22/12, 2023 at 2:37

2

Solved

I can write a function which take a temporary array(such as {1, 2, 3}) as an argument in two ways: // using array template<typename T, int N> auto foo1(const T(&t)[N]) -> void; // us...
Advocacy asked 16/7, 2018 at 1:59

2

This question is related to the one discussed here. I try to use an initializer list to create an argument to be passed to operator[]. #include <string> #include <vector> struct A { ...
Sou asked 9/1, 2012 at 10:23

8

Solved

Why do I receive the error "Variable-sized object may not be initialized" with the following code? int boardAux[length][length] = {{0}};
Epinasty asked 21/6, 2010 at 7:52

3

Solved

I have two vectors: std::vector<int> v1{ 1, 2, 3 }; std::vector<int> v2{ 4, 5, 6 }; I want to create an object of std::initializer_list which holds iterators to the first and last elem...

3

Solved

Let's say you have a variable of type std::vector<std::string> and you initialize it with an initializer list: using V = std::vector<std::string>; V v = { "Hello", "little", "wor...
Chiropractor asked 2/4, 2016 at 19:22

1

I have the following code: #include <iostream> #include <vector> struct C { int a; C() : a(0) {} C(int a) : a(a) {} }; std::ostream &operator<<(std::ostream &os, con...

1

The convenient initializer_list syntax seems to come at a price of not being able to move members of the list, creating unnecessary copies. struct A { // some members which are dynamic resources.....
Rectangular asked 20/3, 2023 at 22:55

5

Solved

In C++11, one can use initializer lists to initialize parameters in functions. What is the purpose of it? Can't the same be done with const vectors? What is the difference of the two programs below...
Crosley asked 3/1, 2015 at 9:30

3

Solved

Can a std::initializer_list contain reference types (both rvalue and lvalue)? Or does one have to use pointers or a reference wrapper (such as std::ref)? EDIT: Perhaps more clarification is due: ...
Salvia asked 8/6, 2014 at 18:28

3

Solved

I'm having trouble with std::initializer_list. I reduced it down to a simple example: #include <initializer_list> #include <cstdio> class Test { public: template <typename type&...
Grados asked 13/1, 2014 at 22:24

8

Solved

If I pass the following code through my GCC 4.7 snapshot, it tries to copy the unique_ptrs into the vector. #include <vector> #include <memory> int main() { using move_only = std::un...
Humbuggery asked 12/12, 2011 at 0:49

1

Solved

I ask because auto deduces{} to be initializer_list. I don't know of any other class in the standard library that the core language depends on like this. You could take out vector or array and C++ ...
Scottie asked 17/11, 2022 at 1:46

3

Solved

std::vector can be initialized as std::vector<std::string> words1 {"the", "frogurt", "is", "also", "cursed"}; Reference Now if want to achieve the similar functionality for one of my ty...
Bascomb asked 9/5, 2013 at 5:31

1

Solved

Let's say I have a template function taking a class object: template<class T> void Foo(T obj); and a class definition as follows: class Bar { public: Bar(int a, bool b): _a(a), _b(b) {} pr...
Oina asked 14/8, 2022 at 14:33

1

Solved

I just read the following article from Raymond Chen's excellent 'The Old New Thing': https://devblogs.microsoft.com/oldnewthing/20210719-00/?p=105454 I have a question about this, best described in...
Leal asked 24/7, 2022 at 0:26

2

Solved

I have found the following behavior of Clang-12, Clang-13 and Clang-14 with c++17 standard: enum class FOO { VALUE }; enum class BAR { VALUE }; FOO value1{BAR::VALUE}; // OK FOO value2 = BAR::V...
Agatha asked 21/6, 2022 at 20:41

7

Solved

Everyone creates std::vector from std::initializer_list, but what about the other way around? eg. if you use a std::initializer_list as a parameter: void someThing(std::initializer_list<int&gt...
Dulaney asked 19/9, 2013 at 13:11

2

Solved

I am trying to use std::initializer_list in order to define and output recursive data-structures. In the example below I am dealing with a list where each element can either be an integer or anothe...
Longrange asked 3/5, 2022 at 18:22

1

Solved

I am facing a relatively tricky situation here that seemed quite easy at first sight. After moving those three members from the parent class Parent to its child class Child it seems that I'm no lon...

1

Solved

In following program, struct C has two constructors : one from std::initializer_list<A> and the other from std::initializer_list<B>. Then an object of the struct is created with C{{1}}:...
Copyhold asked 12/2, 2022 at 19:42

0

Consider following code: run on gcc.godbolt.org #include <initializer_list> struct A { int x; }; int main() { std::initializer_list<A>{100}; } Clang accepts it. GCC and MSVC reject...
Ectropion asked 10/2, 2022 at 19:5

© 2022 - 2024 — McMap. All rights reserved.