initializer-list

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

为君一笑 提交于 2020-01-02 02:43:09
问题 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

Optionally supporting initializer_list construction for templates maybe wrapping containers

狂风中的少年 提交于 2020-01-02 01:15:17
问题 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

Range-based for with brace-initializer over non-const values?

只谈情不闲聊 提交于 2020-01-01 07:31:08
问题 I am trying to iterate over a number of std::list s, sorting each of them. This is the naive approach: #include<list> using namespace std; int main(void){ list<int> a,b,c; for(auto& l:{a,b,c}) l.sort(); } producing aa.cpp:5:25: error: no matching member function for call to 'sort' for(auto& l:{a,b,c}) l.sort(); ~~^~~~ /usr/bin/../lib64/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_list.h:1586:7: note: candidate function not viable: 'this' argument has type 'const std::list<int

Preventing narrowing conversion when using std::initializer_list

做~自己de王妃 提交于 2020-01-01 03:59:07
问题 #include <iostream> struct X { X(std::initializer_list<int> list) { std::cout << "list" << std::endl; } X(float f) { std::cout << "float" << std::endl; } }; int main() { int x { 1.0f }; X a(1); // float (implicit conversion) X b{1}; // list X c(1.0f); // float X d{1.0f}; // list (narrowing conversion) ARG!!! // warning: narrowing conversion of '1.0e+0f' from 'float' to 'int' // inside { } [-Wnarrowing] } Is there any other way of removing std::initializer_list from an overload list (i.e.,

Converting Variadic template pack into std::initializer_list

微笑、不失礼 提交于 2019-12-31 13:55:35
问题 Assume that there is a function which accepts several strings: void fun (const std::initializer_list<std::string>& strings) { for(auto s : strings) // do something } Now, I have a variadic template function say foo() as: template<typename ...Args> void foo () { fun(???); } This method is called externally as: foo<A, B, C, D>(); // where A, B, C, D are classes And these classes which are passed as arguments are expected to contain a common static const member: static const std::string value =

Convert a vector<T> to initializer_list<T>

拈花ヽ惹草 提交于 2019-12-31 08:32:37
问题 Everyone creates std::vector from std::initializer_list , but what about the other way around? eg. if you use a std::initializer_list as a parameter: void someThing(std::initializer_list<int> items) { ... } There are times when you have your items in a vector<T> instead of a literal list: std::vector<int> v; // populate v with values someThing(v); // boom! No viable conversion etc. The more general question is: how to create an stl::initializer_list from a STL iterable, not just std::vector .

Vector initialization with double curly braces: std::string vs int

守給你的承諾、 提交于 2019-12-30 09:54:16
问题 In an answer to this question: Initializing vector<string> with double curly braces it is shown that vector<string> v = {{"a", "b"}}; will call the std::vector constructor with an initializer_list with one element . So the first (and only) element in the vector will be constructed from {"a", "b"} . This leads to undefined behavior, but that is beyond the point here. What I have found is that std::vector<int> v = {{2, 3}}; Will call std::vector constructor with an initializer_list of two

C++: Constructor versus initializer list in struct/class

送分小仙女□ 提交于 2019-12-30 08:15:27
问题 An object of a struct/class (that has no constructor ) can be created using an initializer list . Why is this not allowed on struct/class with constructor ? struct r { int a; }; struct s { int a; s() : a(0) {} }; r = { 1 }; // works s = { 1 }; // does not work 回答1: No, an object with a constructor is no longer considered a POD (plain old data). Objects must only contain other POD types as non-static members (including basic types). A POD can have static functions and static complex data

How to initialize a container of noncopyable with initializer list? [duplicate]

↘锁芯ラ 提交于 2019-12-30 08:06:18
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Can I list-initialize a vector of move-only type? I use gcc 4.6.1 to compile this code int main() { std::vector<std::unique_ptr<int>> vec({ std::unique_ptr<int>(new int(0)), std::unique_ptr<int>(new int(1)), }); return 0; } In what g++ complains there is something like /usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../include/c++/4.6.1/bits/stl_construct.h:76:7: **error: use of deleted function 'std:

how to assign an array from an initializer list

女生的网名这么多〃 提交于 2019-12-30 04:02:51
问题 I have a limited knowledge about c++ . I tried to compile a c++ library and when I run the make file for the following header file mcmc_dhs.h #include <algorithm> #include <map> // intrinsic shape and (reduced) shear just add? //#define WLNOISE // use shear instead of reduced shear for model //#define NOREDSHEAR /// parameters for the M200-concentration relation const number mcreal[2] = {9.59,-0.102}; // Dolag et al. (2004) //const number mcreal[2] = {5.26,-0.100}; // Neto et al. (2007)