list-initialization Questions

3

Solved

Why when I init std::vector with braces std::vector<TS> vec {ts1, ts2}; Compiler call twice copy constructor operator? On the other hand - with push_back it called only once. #include &lt...
Sonni asked 10/12, 2013 at 17:48

2

Solved

I need to define a C++ template that accepts several 3D coordinates as their parameters. When all dimensions of these coordinates are defined as separate integer variables, the parameter list would...

1

Solved

Can you guess the output of this trivial program? #include <vector> #include <string> #include <exception> #include <iostream> int main() { try { struct X { explicit X(...
Happy asked 30/8, 2023 at 21:46

2

C++11 formalized the notion of a narrowing conversion, and disallowed using one at the top level in list-initialization. I am wondering whether, given two types T and U, it is possible for it to b...
Agon asked 19/11, 2012 at 10:14

6

Solved

I have the following shared_ptr to a map: std::shared_ptr<std::map<double, std::string>> and I would like to initialise it using braced-init. Is it possible? I've tried: std::strin...
Flagging asked 6/4, 2016 at 8:32

7

Solved

class C { public: C() : arr({1,2,3}) //doesn't compile {} /* C() : arr{1,2,3} //doesn't compile either {} */ private: int arr[3]; }; I believe the reason is that arrays can be initialized on...

4

Solved

When I run this code: struct X { int a; }; struct Y : public X {}; X x = {0}; Y Y = {0}; I get: error: could not convert ‘{0}’ from ‘<brace-enclosed initializer list>’ to ‘Y’ Why doe...
Tyrothricin asked 7/6, 2013 at 11:55

5

Solved

MyClass a1 {a}; // clearer and less error-prone than the other three MyClass a2 = {a}; MyClass a3 = a; MyClass a4(a); Why?
Duaneduarchy asked 14/8, 2013 at 3:56

2

Solved

I found by accident that the following compiles: #include <string> #include <iostream> class A{ int i{}; std::string s{}; public: A(int _i, const std::string& _s) : i(_i), s(_s...
Siloa asked 10/5, 2021 at 17:53

2

Solved

This code: #include <stdio.h> struct { int i; const char* str; } ar[] = { 1,"asd", //should be {1, "asd"}, 2, "qwe", //should be {2, "qwe"}, 3, &...
Intradermal asked 25/3, 2021 at 12:54

3

Solved

When I am reading The C++ Programming Language 4th Edition, to initialize a variable, the author said it's better to use {} than = to initialize a variable: But I see that there are more peo...
Staffard asked 18/12, 2017 at 2:49

2

Solved

#include <stdio.h> #include <vector> #include <deque> // 1st function void f(int i, int j = 10){ printf("Hello World what"); }; void f(std::vector<int>){ print...

1

Solved

I want to write a function that can be used with an argument that otherwise could directly occur in a range-based loop: template <typename Iterable> void sayIt(const Iterable& stuff) { f...
Taciturn asked 14/8, 2020 at 5:23

2

Solved

These two lines from cppreference What is the difference between these two statements ? I don't see any difference until c++14 If the braced-init-list is empty and T is a class type with a default...
Crosswise asked 28/6, 2020 at 14:13

1

Solved

I am missing something with std::make_shared. Can't it resolve the type of a std::initializer_list, or am I doing something else wrong? #include <vector> #include <memory> class A {};...
Dentation asked 6/4, 2020 at 11:40

1

Solved

In the code below, is there any difference between the two assignments to value? In both cases, would value.v be default constructed, and x be initialized to 42? struct S { std::vector<int>...
Ellery asked 23/1, 2020 at 21:33

1

Solved

I noticed that aggregate list initalization of std::vector performs copy initialization when move is more applicable. At the same time, multiple emplace_backs do what I want. I could only come up ...
Beanery asked 8/1, 2020 at 23:52

1

Solved

This is very similar to a question I asked earlier today. However, the example I cited in that question was incorrect; by mistake, I was looking at the wrong source file, not the one that actually ...
Saree asked 26/4, 2017 at 3:17

1

Solved

I am trying to initialize an std::unordered_map using the constructor that accepts data through an initialization list and the initial number of buckets. For some reason, that constructor works i...
Haleyhalf asked 27/10, 2019 at 8:9

3

Solved

https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique writes that std::make_unique can be implemented as template<typename T, typename... Args> std::unique_ptr<T> make_unique...
Butacaine asked 13/3, 2019 at 12:10

4

I'm somewhat new to c++ programming. I couldn't find my answer any where on google so hopefully it can be answered here. is there a difference between the following unsigned int counter{ 1 }; o...
Watteau asked 19/9, 2019 at 11:35

2

Solved

I have fallen into the belief that variables are assigned their default values when using brace initialization. But I was wrong. In the following example: #include <string> #include <io...
Starvation asked 27/6, 2019 at 4:30

1

Solved

when constructing variables using the list-initialization (like int x{ 5 };) the standard §8.5.4 says: If a narrowing conversion […] is required to convert any of the arguments, the program is i...
Tel asked 19/6, 2019 at 10:42

1

This code compiles: std::string f(bool a, std::string const& b) { if (a) return b; return {}; } This code also compiles: std::string f(bool a, std::string const& b) { return a ...

1

The following code compiles fine in Clang and outputs size of int [3] array #include <iostream> int main() { const int (&a)[] = { 1, 2, 3 }; std::cout << sizeof a << std::...
Ridley asked 5/4, 2019 at 22:26

© 2022 - 2024 — McMap. All rights reserved.