Questions tagged [list-initialization]

In C++11 list-initialization refers to initializing a variable using curly braces

Filter by
Sorted by
Tagged with
1 vote
3 answers
148 views

Is char s[] = {"ABC"}; legal?

#include <stdio.h> int main(){ char s[] = {"hello"}; printf("%s", s); return 0; } This code can be compiled and executed successfully,but how can one dimension ...
GongNanyue's user avatar
0 votes
1 answer
93 views

List-initialization of vector of pairs

Note: Please don't close the question just because the root cause was due to comma operator. The value of the question was to let the community understand the failure of: std::vector<std::pair<...
cpp's user avatar
  • 291
-1 votes
1 answer
137 views

c++ vector = {vec1.begin, vec1.begin} returns empty vector

I'm trying to understand the behavior of creating a subvector of another vector. For example, this code: https://godbolt.org/z/PYG34vnTr has a 2-element vector vec1. I want to create a vector of the ...
jkang's user avatar
  • 511
3 votes
0 answers
111 views

C++: Rules for this return {} initialization

I am trying to understand the rules governing the following initialization: struct A { const char* s_ptr; std::string str; }; A Foo() { return {}; } A a = Foo(); As per my understanding, a....
cbhattac's user avatar
  • 181
0 votes
0 answers
47 views

std::find in vector with structures

I have a structure struct position { int x; int y; }; and a vector pos of such structures. When I try to check whether a certain element is present in this vector, I use auto it = std::...
user43283's user avatar
  • 203
0 votes
0 answers
60 views

Vector Initialisation - C++ -- getting error while initialising using list

vector<int> arr3 = {3, 6, 9, 12, 15}; I'm trying to initialise the vector by above method and it is throwing the following error: vectors.cpp:102:16: error: non-aggregate type 'vector<int>...
Jatin Bawne's user avatar
25 votes
1 answer
2k views

Constructing a vector of structs (with some custom constructors) from exactly two string literals crashes. Why?

Can you guess the output of this trivial program? #include <vector> #include <string> #include <exception> #include <iostream> int main() { try { struct X { ...
Sz.'s user avatar
  • 3,409
1 vote
3 answers
95 views

gcc no error with extra comma in array initializer

Here's an example: int main () { int a[] = {1, 2, 3,}; } Note the extra , after 3. There is no warning even with -Wall and everything. I noticed this while reading some coreutils code that seemed ...
user129393192's user avatar
-2 votes
1 answer
58 views

Aggregate initialization with nested map doesn't work expectedly

It seems I was a bit fuzzy when posing this question since it have been a few days that I'm trying to fix this problem and couldn't reproduce it as an MRE probably the issue is somewhere else in my ...
ניר's user avatar
  • 1,234
0 votes
0 answers
104 views

Why does initialization of vector of class with implicit string constructor from string literal fail?

The following code fails to compile. #include <iostream> #include <map> #include <vector> using namespace std; class mc { string s; public: mc(const std::string s) : s{...
darmual's user avatar
  • 249
1 vote
1 answer
65 views

how to understand list initialization in C++ when use it to construct class object?

As we all know, list initialization is introduced to C++ from C++11. In most cases, it is easy to understand. However, when using list initialization to construct a class object, it always makes me ...
csbo's user avatar
  • 53
0 votes
1 answer
102 views

from '<brace-enclosed initializer list>' to X: understanding list initialization in constructors

It seems I don't understand braced init lists at all. Why does the following compile for operator=() (entity e) but not for the constructor (entity f)? Demo #include <cstdio> #include <...
glades's user avatar
  • 3,945
1 vote
1 answer
110 views

GCC fails to select the expected overloaded operator=() when using an empty initializer list

I want to design something like a wrapper class for any type T. All that's required is for my class to support assignment of values. So, consider the following simplified code: template<typename T&...
guard3's user avatar
  • 858
1 vote
2 answers
357 views

Pass initializer list to function for initialization of std::array

In my Point header I have: 15 template<typename real> class Point 16 { 17 public: 18 // Constructors 19 Point(); 20 Point(const std::initializer_list<real>&); 21 ...
drjrm3's user avatar
  • 4,504
0 votes
1 answer
72 views

Why does list-Initialized object using default constructor compile in gcc9 but not in gcc5.1?

Why does following code compiles in gcc9 but not in gcc5.1? struct AAA { int xxx = 1; }; int main() { AAA p; new AAA{p}; ...
mathworker's user avatar
0 votes
1 answer
225 views

how to initialize a single value to whole array in cpp [duplicate]

I'm trying to initialize a single value to whole array in c++. For ex:- I want to initialize 1 in whole array by writing it by only once I have tried to initializing 1 in whole array but it throws ...
Astha Tomar's user avatar
0 votes
2 answers
131 views

Function was put into a curly bracket in order to initialize the member in class. What is its syntax?

The code is shown here: class Basket{ public: /*other contents*/ private: // function to compare shared_ptrs needed by the multiset member static bool compare(const std::shared_ptr<Quote>...
岁暮音书's user avatar
0 votes
1 answer
71 views

cuda does not compile member initialization on derived classes?

the following simple code does not compile in Cuda 11.8, I get error : too many initializer values But that is very standard C++, right? Is there some setting or switch to get this compiled? The ...
ray_ray_ray's user avatar
1 vote
1 answer
104 views

Does value-initialization use the implicit default constructor? [duplicate]

According to this site /link/: If the default constructor is explicitly declared but marked as deleted, empty brace initialization can't be used: and it also gives an example to this: class class_f {...
Fox1942's user avatar
  • 286
0 votes
1 answer
249 views

How to list-initialize an initializer_list from another initializer_list

When I'm trying to compile the following code, the compiler complains: int main(void) { std::initializer_list<int> lst1{}; std::initializer_list<int> lst2{lst1}; // error } The ...
mada's user avatar
  • 1,812
1 vote
1 answer
132 views

perfect forwarding with brace enclosed initialiser list

For personal education I am coding up a basic implementation of a hash table (although the below is probably relevant to any container that is holding types that can be list initialised) and want to ...
user3353819's user avatar
0 votes
1 answer
447 views

Is there any way to append initializer list --> std::initializer_list<std::pair<std::string, std::string>>?

In my project there is need to append initializer list at runtime. I have figured the way to have initializer_list std::initializer_list<std::pair<std::string, std::string>> at runtime in ...
Rishabh Madan's user avatar
-1 votes
2 answers
79 views

C++, Too many initializer values, Bool Array Initialization using DMA Pointers

I don't understand why am i getting this error. Is it not the correct way of array initialization? #include <iostream> using namespace std; int main() { bool **arr = new bool *[5]; //rows ...
Syeda Rabia Hashmi's user avatar
5 votes
2 answers
100 views

Direct initialization with prvalue: Bug in MSVC?

Consider the following code: struct S { S(int, double) {} explicit S(const S&) {} explicit S(S&&) {} }; void i_take_an_S(S s) {} S i_return_an_S() { return S{ 4, 2.0 }; } ...
Ruperrrt's user avatar
  • 499
2 votes
2 answers
1k views

Brace initialization of Eigen matrix

Using gcc 9.4.0 and Eigen 3.3.7, the following compiles without issue: #include <Eigen/Dense> int main(int Argc, char *Argv[]) { Eigen::Matrix<int, 1, 4> d = {1, 2, 3, 4}; } ...
John Barrabas's user avatar
0 votes
0 answers
27 views

List-initialization to Fixed-Sized C-Array zeroes unused indices [duplicate]

I have a constructor that looks like A (const int(&inArr)[4]) { ... } And I can construct an instance with list-initialization as such, A a({3,4,5,6}); However, I can also construct an ...
Mark A. Ropper's user avatar
3 votes
2 answers
95 views

Rust more compact list initialisation?

I commonly have to work with geometric data, in C++ it was normal for me to do things like this: struct Vertex { vec2;} vector<Vertex> triangle = {{-1, 0}, {0,1}, {1, 0}}; Which is fairly ...
Makogan's user avatar
  • 8,521
-1 votes
1 answer
124 views

List-Initialization of a Subclass [duplicate]

I want to initialize a class B that is a subclass of A by list-initialization (https://en.cppreference.com/w/cpp/language/list_initialization) However, it does not work like this: struct A { int x; ...
Urquhart's user avatar
  • 139
0 votes
1 answer
84 views

c++11 - list-initialization of an aggregate from an aggrrgate

On this page of the cppreference.com I read the following: If T is an aggregate class and the braced-init-list has a single element of the same or derived type (possibly cv-qualified), the object is ...
Anton Tretyakov's user avatar
1 vote
0 answers
219 views

Short lambda being split by clang-format, even with AllowShortLambdasOnASingleLine

I have this code block: int main() { SomeType something1( [](const Input& i) -> Output { return Output {1, 2}; }, [](const Input& i) -> Output { return Output {1, 2}; ...
digitalPhonix's user avatar
1 vote
1 answer
65 views

List initialization rules [duplicate]

I want to initialize std::vector<char> with count and value. This works: int n = 100; std::vector<char> v(n, 0); However, list initialization std::vector<char> v{n, char(0)}; gives ...
user2052436's user avatar
  • 4,441
2 votes
1 answer
247 views

What is the cause of his C++20 brace initialization backwards compatibility failure? [duplicate]

I found an interesting case where valid(?) C++17 code fails when compiling with C++20. The simplest reproduction is a struct with an explicit default constructor. On C++17, this code works fine. In C++...
BWG's user avatar
  • 2,248
2 votes
1 answer
88 views

Understanding how compiler uses plain {}-list inside initialization

Consider the following toy code: class Y { public: Y(int, int) { cout << "Y ctor\n"; } }; class X { public: //X(initializer_list<int>) { cout << "init\n";...
CPPL's user avatar
  • 726
-1 votes
1 answer
128 views

giving an uninitialized array values in c [duplicate]

My question is very basic, but I couldn't find anything about it. I have an array of pointers. I want to assign it like this mat* quickAccessMatrices[6] = {&MAT_A,&MAT_B,&MAT_C,&MAT_D,&...
PlainXYZ's user avatar
  • 136
0 votes
1 answer
31 views

Getting error while trying to read filenames from array [closed]

#include <iostream> #include <fstream> #include <iomanip> #include <cstdlib> using namespace std; int main() { int numberArray[] = {}; int atoi(const char * str); /...
Vigilant Straight's user avatar
1 vote
4 answers
95 views

What is difference of char l[] {'try'} and char l[] {'t', 'r', 'y'} in c++?

When I was trying to cout them char l[] {'t', 'r', 'y'}; std::cout << l << std::endl; I got try printed in the terminal. However, when I tried this. char l[] {'try'}; I only got y. I ...
user avatar
0 votes
1 answer
98 views

Seg Fault from dictionary initialization Python

So I am working on a project that deals with a large number of vehicles and transmissions between those vehicles. I have a working code that works well for small numbers of vehicles, but when I start ...
deadskin's user avatar
0 votes
2 answers
84 views

How to pre-initialize an object array in c++ like an actual array?

DISCLAIMER Good day, I am a novice programmer so please don't judge me if I have any mistakes that may be obvious or not. I have a code that looks like this. class Book{ public: string author; ...
nyawng's user avatar
  • 9
1 vote
1 answer
220 views

Is direct-initialization equivalent to direct-list-initialization?

I have the following example: struct S{ int x, y; } S s1{1}; // direct-initialization or direct-list-initialization ? S s2{1, 2}; // direct-initialization or direct-list-initialization ? S s3(1)...
mada's user avatar
  • 1,812
2 votes
3 answers
766 views

how to brace initialize vector of custom class in C++?

Having this simple code: #include <iostream> #include <vector> #include <string> class Person{ public: Person(std::string const& name) : name(name) {} std::string const&...
milanHrabos's user avatar
  • 2,071
0 votes
1 answer
64 views

What is the difference between first and second version of the constructor

The first 1 version works correct. But why doesn't the code work with the second 2 version? #include <mutex> #include <queue> #include <thread> using namespace std; template <...
vaprvsapr's user avatar
3 votes
1 answer
334 views

Automatic generation of a brace-enclosed initializer list in C++ using language features (NOT pre-processor directives)

I'm looking for a solution using only native C++ language features (up to C++17) for accomplishing the following: std::array<Type, unsigned int Elem> array_{Type(), // 1 - Call constructor on ...
Chris Rajula's user avatar
0 votes
1 answer
406 views

structure errror in C error: expected identifier or ‘(’ before ‘.’ token

#include <stdio.h> typedef struct car{ unsigned char buf[1024]; int index; int hdr; int len; int dlms_detected; }frma ; int main() { frma dms; dms....
dcu's user avatar
  • 1
2 votes
1 answer
76 views

Can a braced initializer be used for non-type template argument in C++? [duplicate]

In the next program the second non-type template argument of struct A is initialized with {} in the alias template B<T>: template<class T, T> struct A{}; template<class T> using B = ...
Fedor's user avatar
  • 17.7k
2 votes
1 answer
96 views

C# How to call the inner list constructor inorder to define capacity

I would like to access the inner list constructor inorder to define capacity, how could i achieve this? This is what i have: this will initialize the outer list to 20 elements. public List<List<(...
Kaur Kukk's user avatar
3 votes
0 answers
80 views

default initialization, value initialization and in-class-initialization of object not getting properly?

You might feel code is long and I write lot of things but believe me it is quite simple. I referred these two answers on stack-overflow for understanding this. post1 & post2 Code #include <...
Abhishek Mane's user avatar
2 votes
3 answers
136 views

Do I need to define some constructor in my class to enable assignment with `{}`

From C++11 and above, I can construct a class with list initialization std::vector<int> foo{1,2,3} or copy list-initialization with std::vector<int> foo = {1,2,3}. Assuming I have a class ...
kz28's user avatar
  • 771
-2 votes
1 answer
230 views

How do I write this pseudocode in C++? Function returns two arrays to two initialized arrays

I am trying to implement a bottom up approach function to the rod cutting problem and I need to use this particular pseudo-code from the CLRS textbook. In it there two functions and one calls the ...
piesoda's user avatar
4 votes
1 answer
149 views

C++ initialization strange behavior

typedef struct node { int val; int val2; node(int a, int b) : val(a), val2(b) {} node(int val) = delete; }node; int main() { node a = {3}; cout << a.val <&...
syacer's user avatar
  • 157
0 votes
1 answer
225 views

List initialization of function template arguments with automatic template type deduction

Here's the setup to illustrate the problem. template <typename T> struct opt { const char* name; T x; template <typename X> opt(const char* name, X&& x) : name(name), x(...
SU3's user avatar
  • 5,186

1
2 3 4 5
7