63 questions
2 votes
1 answer
119 views
C++ Braced Initialization Not Triggering Expected Conversion Operator
I'm working with a C++ class that includes both a conversion operator and a constructor that takes a std::initializer_list. According to Scott Meyers in "Effective Modern C++," Item 7, ...
0 votes
0 answers
57 views
Is it undefined behavior to access the elements of a list-initialized std::initializer_list after it has been destroyed? [duplicate]
The following code when compiled with -fsanitize=address emits a stack-use-after-scope error in GCC 12.3 and Clang 20.1, but not GCC 14.2. #include <functional> #include <cstdint> #include ...
0 votes
0 answers
63 views
Should a constructor with a std::initializer_list be explicit?
I am designing a container (yeah, do not reinvent the wheel, I know). Let's call it Container<T>. I have a constructor to allow the initial number of elements: explicit Container(std::size_t ...
2 votes
1 answer
160 views
Catching dangerous use of std::initializer_list
#include <initializer_list> int main() { std::initializer_list<int> il = std::initializer_list<int>{1, 2, 3}; return 0; } As I understood it you should not copy std::...
0 votes
2 answers
82 views
How to initialize an intializer_list?
Some cpp class accept initializer_list as input, for example: std::map<std::string, int> m{{"CPU", 10}, {"GPU", 15}, {"RAM", 20}}; or std::map<std::string, int&...
0 votes
2 answers
277 views
Initialize std::list<CustomType> from std::initializer_list<std::string_view> in ctor's member initializer list
I have a Game class that stores the m_players as a data member (std::list<Player>) and each player has multiple data members, one of them being their m_name (std::string). When instantiating a ...
0 votes
1 answer
118 views
How can you tell whether list-initialization calls a std::initializer_list constructor? [closed]
My question is about the confusion in C++ default class member initialization between using an initializer list and calling a constructor. There is no ambiguity in the language but a possible ...
3 votes
0 answers
402 views
initializing from empty std::initializer_list vs default constructor
I've got a class that basically contains a std::vector. I can default-construct an object of this class, leaving the contained std::vector empty. I can also value-construct it from a set of values, ...
6 votes
1 answer
208 views
braced initialization of std::vector of move-only objects fails [duplicate]
I can concisely (with braces) initialize 5 out of 6 of the following cases: of copyable of move-only array YES YES std::array YES YES std::vector YES NO The one case that doesn't seem to work is ...
10 votes
1 answer
627 views
When can a std::initializer_list be constexpr?
According to cppreference.com, std::initializer_lists have constexpr constructors and constexpr size methods (since C++14). Although the compiler I was using seemed to agree that the size of a ...
1 vote
1 answer
224 views
C++: static initializer_list transformation
C++ standard defines that initializer_list will be transformed in the following manner: struct X { X(std::initializer_list<double> v); }; X x{ 1,2,3 }; The initialization will be implemented ...
2 votes
2 answers
1k views
How to convert initializer list to another type and add an item, in compile time?
I wanted to figure this out, because I was running some unit tests and our system allows you to mark the test with one or more initializer lists, which will translate to multiple runs with different ...
0 votes
1 answer
119 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 <...
2 votes
3 answers
232 views
Initializer list issue in constructor
I have a hard time understanding how std::initializer_list works. I checked other questions, but found nothing relevant (or maybe I didn't see it?). Say I have this: template<typename T> struct ...
0 votes
1 answer
94 views
function-style-cast in Template Meta programming
Code taken from the book Template Metaprogramming with C++, it doesn't compile i tried visual studio 22 i get the error: Error C2440 \<function-style-cast\>' : cannot convert from '...