initializer-list

What is the underlying structure of std::initializer_list?

不问归期 提交于 2019-12-07 03:13:24
问题 First part : std::initializer_list is a really helpful feature of C++11, so I wondered how it is implemented in the standard library. From what I read here, the compiler creates an array of type T and gives the pointer to the initializer_list<T> . It also states that copying an initializer_list will create a new object referencing the same data : why is it so ? I would have guessed that it either : copies the data for the new initializer_list moves ownership of the data to the new initializer

Implementing a std::array-like container with a C++11 initializer_list

北战南征 提交于 2019-12-06 23:01:30
问题 The only and imo very inconvenient caveat of std::array is that it can't deduce its size from the initializer list like built-in C arrays, it's size must be passed as a template. Is it possible to implement a std::array-like container (a thin wrapper around a built-in C array) with a C++11 initializer_list? I ask because, unlike std::array, it would automatically deduce the size of the array from the initializer list which is a lot more convenient. For example: // il_array is the hypothetical

why does `vector<int> v{{5,6}};` work? I thought only a single pair {} was allowed?

女生的网名这么多〃 提交于 2019-12-06 19:51:35
问题 Given a class A with two constructors, taking initializer_list<int> and initializer_list<initializer_list<int>> respectively, then A v{5,6}; calls the former, and A v{{5,6}}; calls the latter, as expected. (clang3.3, apparently gcc behaves differently, see the answers. What does the standard require?) But if I remove the second constructor, then A v{{5,6}}; still compiles and it uses the first constructor. I didn't expect this. I thought that A v{5,6} would be the only way to access the

Is this unsafe usage of a braced initializer list?

谁说胖子不能爱 提交于 2019-12-06 13:32:12
I had a bug crop up in my program recently that surprised me a bit, but perhaps it shouldn't, with the vast number of types of initialization that C++ (especially modern C++) provides. I have a class that looks something like this: struct foo { foo(const std::set<int> &values) { values_ = values; } set::set<int> values_; }; When constructing foo s, it's usually easiest to specify the set of values inline using an initializer list (an instance will typically have 2-3 known values that are known at compile time), like this: auto f = std::make_shared<foo>({ 1, 2, 3 }); However, I recall that when

Is it possible to initialize a vector with increasing values in a single line?

不想你离开。 提交于 2019-12-06 08:15:30
问题 Is it possible to merge the two initialization lines into a single statement with the help of initializer lists or other C++ features? The vector values always increment with one, but the size n is not fixed. #include <numeric> #include <vector> #include <iostream> int main() { int n = 10; // Can the two lines below be combined into a single statement? std::vector<int> v(n); std::iota(v.begin(), v.end(), 1); for (int i : v) std::cout << i << std::endl; return 0; } 回答1: You can use Boost

Direct initialization with empty initializer list

為{幸葍}努か 提交于 2019-12-05 23:10:38
问题 struct X { X() { std::cout << "default ctor" << std::endl; } }; int main() { X({}); } This prints out default ctor and that makes sense because empty brace value-initializes the object (I think). However, struct X { X() { std::cout << "default ctor" << std::endl; } X(std::initializer_list<int>) { std::cout << "initializer list" << std::endl; } }; int main() { X({}); } For this, I got initializer list I don't find this behavior so strange, but I'm not fully convinced. What is the rule for this

Can operators be overloaded for initializer_list literals? [duplicate]

丶灬走出姿态 提交于 2019-12-05 19:38:45
问题 This question already has an answer here : Initializer lists and RHS of operators (1 answer) Closed 2 years ago . I am trying to overload operators for std::initializer_list , but the following compiles neither in GCC 4.7.2 nor Clang 3.2: #include <initializer_list> void operator+(const std::initializer_list<int>&, const std::initializer_list<int>&); int main() { {1, 2} + {3, 4}; } 13.5/6 says that an operator function shall have at least one parameter whose type is a class, enum, or

Initializing struct vector with brace-enclosed initializer list

北战南征 提交于 2019-12-05 17:37:11
问题 I initialize normal-type vectors like this: vector<float> data = {0.0f, 0.0f}; But when I use structure instead of normal-type struct Vertex { float position[3]; float color[4]; }; vector<Vertex> data = {{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f}}; I get error could not convert '{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f, 0.0f}}' from '<brace-enclosed initializer list>' to 'std::vector<Vertex>' . What's wrong with this? 回答1: A set of {} is missing: std::vector<Vertex> data = { // for the vector

std::initializer_list<> and a Reference Parameter

Deadly 提交于 2019-12-05 17:22:52
I'm new to using initializer lists and I'm wondering if they work similar to other stl containers. By that I mean do they copy values? What I'm trying to do is a simple min() function like this: template <class T> T& minArgs(const std::initializer_list<T&>& Arguments) { const T* Smallest = Arguments.begin(); for (const T* I = begin(Arguments); I != end(Arguments); ++I) { if (*I < *Smallest) Smallest = I; } return *Smallest; } However when I call the function I get this from GCC: error: 'const' qualifiers cannot be applied to 'int&' I've been playing around with this and it seems initializer

Can't copy a std::vector<std::function<void ()>> using uniform initialization. Is this correct?

吃可爱长大的小学妹 提交于 2019-12-05 16:13:43
问题 The following code does not compile in GCC 4.7.2 or Clang 3.2: #include <vector> #include <functional> int main() { std::vector<std::function<void()>> a; std::vector<std::function<void()>> b{a}; } The issue is that the compiler will try to create b using an initializer_list, when clearly it should just be calling the copy constructor. However this seems to be desired behavior because the standard says that initializer_list constructors should take precedence. This code would work fine for