initializer-list

For constructors, how do I choose between variadic-templates vs std::initializer_list?

╄→гoц情女王★ 提交于 2019-12-04 18:05:25
问题 In the current state of c++11 (say gcc 4.7.2), how should I choose between using a variadic-template or a std::initializer_list when I need a constructor that can take variable arguments? 回答1: A variadic template allows you providing arguments of different types, while an std::initializer_list is templated with the type of the argument. This means the type of all the elements in the list must be the same (or convertible to the underlying type, but no narrowing conversions are allowed).

Initialize a constant sized array in an initializer list

▼魔方 西西 提交于 2019-12-04 15:35:13
问题 I've got a situation which can be summarized in the following: class Test { Test(); int MySet[10]; }; is it possible to initialize MySet in an initializer list? Like this kind of initializer list: Test::Test() : MySet({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) {} Is there any way to initialize a constant-sized member array in a class's initalizer list? 回答1: While not available in C++03, C++11 introduces extended initializer lists . You can indeed do it if using a compiler compliant with the C++11

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

一曲冷凌霜 提交于 2019-12-04 13:00:57
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; } You can use Boost.counting_iterator for this: std::vector<int> v(boost::counting_iterator<int>(1), boost::counting_iterator<int>(n

Why I can not return initializer list from lambda

喜欢而已 提交于 2019-12-04 09:57:28
问题 Why this code is not valid? auto foo=[](){ return {1,2}; }; However, this is valid since the initializer list is used just to initialize a vector not to return itself: auto foo=[]()->std::vector<int>{ return {1,2}; }; Why I can not return initializer list ? It could be useful. For example, a lambda that can be used to initialize a vector or a list or ... with some default values for something. 回答1: Lambda return type deduction uses the auto rules, which normally would have deduced std:

Using multidimensional std::initializer_list

流过昼夜 提交于 2019-12-04 09:53:00
I have a question about the use of multidimensional std::intializer_list in C++. I have a Matrix class, and I want to be able to initialize it like this: Matrix<int, 3, 3> m({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}); The constructor that I have now takes an argument of a two-dimensional initializer list, but the compiler doesn't like how I'm using it. Here's the code: template<typename T, unsigned int rows, unsigned int cols> Matrix<T, rows, cols>::Matrix(std::initializer_list<std::initializer_list<T> > set) { std::vector<std::initializer_list<T> > setVec = set; std::vector<std::vector<T> > v; for

Array Equivalent of Bare-String

拈花ヽ惹草 提交于 2019-12-04 07:09:58
问题 I can do this without issue: const char* foo = "This is a bare-string"; What I want is to be able to do the same thing with an array: const int* bar = {1, 2, 3}; Obviously that code doesn't compile, but is there some kind of array equivalent to the bare-string? 回答1: You can't do this: const int* bar = {1, 2, 3}; But you can do this: const int bar[] = {1, 2, 3}; Reason is that char* in C (or C++) have an added functionality, besides working as a char pointer, it also works as a "C string",

Is initializing with “var{args}” a new feature of C++0x, or merely syntactic sugar?

你离开我真会死。 提交于 2019-12-04 04:19:58
问题 I was reading the C++0x faq and came across the section detailing initializer lists. The examples were mostly variations of: vector<int> vi = { 1, 2, 3 }; vector<int> vj({1, 2, 3}); // etc. However, also listed was the form: vector<int> vk{2}; This form appears elsewhere in the faq, and I am curious as to whether it is semantically different from the initial two forms, or just syntactic sugar for vk({x, y, z}) . 回答1: One is uniform initialization , and the other is initializer lists . They

Initializing struct vector with brace-enclosed initializer list

 ̄綄美尐妖づ 提交于 2019-12-04 03:29:45
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? A set of {} is missing: std::vector<Vertex> data = { // for the vector { // for a Vertex {0.0f, 0.0f, 0.0f}, // for array 'position' {0.0f, 0.0f, 0.0f, 0.0f} // for array 'color'

Can operators be overloaded for initializer_list literals? [duplicate]

南笙酒味 提交于 2019-12-04 03:13:22
This question already has an answer here: Initializer lists and RHS of operators 1 answer 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 reference to either, and the standard specifies initializer_list as a template class, so to me it seems like it should be conformant.

C++0x initializer list example

馋奶兔 提交于 2019-12-04 03:10:37
I would like to see how this example of existing code would be able to take advantage of the C++0x initializer list feature. Example0: #include <vector> #include <string> struct Ask { std::string prompt; Ask(std::string a_prompt):prompt(a_prompt){} }; struct AskString : public Ask{ int min; int max; AskString(std::string a_prompt, int a_min, int a_max): Ask(a_prompt), min(a_min), max(a_max){} }; int main() { std::vector<Ask*> ui; ui.push_back(new AskString("Enter your name: ", 3, 25)); ui.push_back(new AskString("Enter your city: ", 2, 25)); ui.push_back(new Ask("Enter your age: ")); } Would