initializer-list

C++11 Implicit conversion from initialization list to array parameter

可紊 提交于 2019-12-05 14:01:50
In C++11, is it possible to do something similar to the following? template<typename T, size_t N> void foo(array<T, N> src) { ... } ... foo({1, 2, 3}) I'm currently running GCC 4.8. Ali Yes , I managed to get the following work (since you allow something similar ): template<typename T, size_t N> void foo(array<T, N> src) { ... } ... foo('a', 'b'); foo(1, 2, 3); Here is how: #include <array> #include <iostream> #include <utility> using namespace std; template<typename T, unsigned long N> void foo(array<T,N> src) { for (auto e : src) cout << e << endl; } template<class T, class... Tail> auto

Initializing private std::array member in the constructor

淺唱寂寞╮ 提交于 2019-12-05 12:44:01
I was wondering what is the proper way to initialize a std::array member of the class in the constructor, when the initial array values are parameters to the constructor? More specifically, consider the following example: class Car { public: Car(const std::string& color, int age): color_(color), age_(age) {} // ... private: std::string color_; int age_; }; class ThreeIdenticalCars { private: std::array<Car, 3> list; public: ThreeIdenticalCars(const std::string& color, int age): // What to put here to initialize list to 3 identical Car(color,age) objects? {} }; Obviously one way is to write

What is the underlying structure of std::initializer_list?

不想你离开。 提交于 2019-12-05 07:44:49
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_list Second part : From just one of many online references for the std::vector constructors: vector

Difference between std::vector and std::array initializer lists

拟墨画扇 提交于 2019-12-05 05:46:47
This C++11 code works fine for me: #include <iostream> #include <vector> #include <array> using namespace std; struct str { int first, last; }; vector<str> fields { {1,2}, {3,4}, {5,6} }; int main() { for (str s : fields) cout << s.first << " " << s.last << endl; } It prints the six expected values. But if I change vector<str> to array<str,3> , gcc gives me this error: "too many initializers for ‘std::array’". If I change the initialization of fields thus: array<str,3> fields { str{1,2}, str{3,4}, str{5,6} }; Things work nicely. So why do I need str{1,2} when using std::array , but only {1,2}

How to do nested initializer_lists in visual C++ 2013

假装没事ソ 提交于 2019-12-05 05:45:24
I've got a program which works in g++ and clang, using a nested initializer_list. In Visual C++, the 1D case works, but a 2D nested initializer_list does not. Is there a trick to make Visual C++ work, or is this maybe a bug in their implementation? Here's my example code. It works in Visual C++ 2013 if I remove the annotated line. #include <iostream> #include <initializer_list> using namespace std; template<class T> void print(T val) { cout << val; } template<class T> void print(initializer_list<T> lst) { bool first = true; cout << "["; for (auto i : lst) { if (!first) cout << ", "; print(i);

Could not convert from brace-enclosed initializer list to std tuple

醉酒当歌 提交于 2019-12-05 04:35:57
As part of a bigger project, I'm playing with std::tuple and templates; consider the following code: template <typename ...T> void foo(tuple<T...> t) {} void bar(tuple<int, char> t) {} tuple<int, char> quxx() { return {1, 'S'}; } int main(int argc, char const *argv[]) { foo({1, 'S'}); // error foo(make_tuple(1, 'S')); // ok bar({1, 'S'}); // ok quxx(); // ok return 0; } According to this answer C++17 supports tuple initialization from copy-list-initialization , however it seems such support is limited since I get the following error (GCC 7.2.0): main.cpp: In function 'int main(int, const char*

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

一世执手 提交于 2019-12-05 03:53:15
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 container // automatically deduces its size from the initalizer list il_array <int> myarr = {2, 4, 6,

Constructor for nested initializer lists

核能气质少年 提交于 2019-12-05 01:43:03
问题 Is it possible to have a generic constructor that takes any type of initializer list, even if this has nested lists within? Say you have the following partial template specialization for a class that takes in its constructor nested initializer lists: template class ClassA; template <> class ClassA<4> { typedef std::initializer_list<double> list_type; typedef std::initializer_list<list_type> llist_type; typedef std::initializer_list<llist_type> lllist_type; typedef std::initializer_list<lllist

Optionally supporting initializer_list construction for templates maybe wrapping containers

对着背影说爱祢 提交于 2019-12-05 01:38:13
If I have a template that wraps a standard container, it seems I can reasonably easily delegate the initializer_list constructor: template<typename T> struct holder { T t_; holder() : t_() {} holder(std::initializer_list<typename T::value_type> values) : t_(values) {} }; So this works nicely with std::vector, for instance. int main(int argc, char* argv[]) { holder<std::vector<int>> y{1,2,3}; return EXIT_SUCCESS; } But it pretty obviously doesn't work for T as 'int', or any other type that doesn't have a nested value_type typedef. So, I'd like to use some sort of enable_if or similar trick to

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

╄→гoц情女王★ 提交于 2019-12-05 01:34:55
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 initializer_list<int> constructor. (I discovered this while playing around with std::vector and this