initializer-list

Initializer list vs. vector

落爺英雄遲暮 提交于 2019-12-20 10:40:27
问题 In C++11, one can use initializer lists to initialize parameters in functions. What is the purpose of it? Can't the same be done with const vectors? What is the difference of the two programs below? Using an initializer list: #include <iostream> using namespace std; int sumL(initializer_list<int> l){ int sum = 0; for (const auto i: l){ sum += i; } return sum; } int main(){ cout << sumL({1, 2, 3}) << "\n"; return 0; } Using a const vector: #include <iostream> #include <vector> using namespace

Constructor from initializer_list

 ̄綄美尐妖づ 提交于 2019-12-20 03:24:18
问题 I am implementing a container in c++, a wrapper for an array in fact. I am not sure how to implement a constructor from initializer_list. I end up with this implementation but it seems to me really ugly. So could be an array allocated in a heap initialized by an initializer_list. Or is there an elegant way how to do it? template <typename T> class sequence { public: sequence (size_t n): _data {new T[n]}, _size {n} {}; sequence (std::initializer_list<T> source); ~sequence() { delete[] _data; }

Using a macro to initialize a big array of non-Copy elements

家住魔仙堡 提交于 2019-12-20 02:47:34
问题 I'm trying to initialize a big array of elements with the same initializer. 64 elements is just an example — I want to make it at least 16k. Unfortunately a simple let array : [AllocatedMemory<u8>; 64] = [AllocatedMemory::<u8>{mem:&mut []};64]; won't work because the AllocatedMemory struct does not implement Copy error: the trait `core::marker::Copy` is not implemented for the type `AllocatedMemory<'_, u8>` [E0277] let array : [AllocatedMemory<u8>; 64] = [AllocatedMemory::<u8>{mem:&mut []};

Forcing std::vector overload instead of int overload on list with one element

五迷三道 提交于 2019-12-20 01:17:10
问题 Consider the code below: #include <iostream> #include <vector> void f(std::vector<int> v) {std::cout << __PRETTY_FUNCTION__ << std::endl;} void f(int n) {std::cout << __PRETTY_FUNCTION__ << std::endl;} int main() { f({42}); // the int overload is being picked up } Live on Coliru I was a bit surprised to realize that in this case the int overload is being picked up, i.e. the output of the program is: void f(int) with the warning warning: braces around scalar initializer [-Wbraced-scalar-init]

Lifetime Extension of a initializer_list return

给你一囗甜甜゛ 提交于 2019-12-19 07:43:06
问题 So I have a lambda who's return type is auto and I'm having issues with the array backing for an initializer_list being destroyed here: const auto foo = [](const auto& a, const auto& b, const auto& c) { return {a, b, c}; }; I will be using the lambda like this: auto bar = foo(1, 2, 3); for(const auto& i : bar) cout << i << endl; A job that I'm working has as part of their coding standard that all lambdas are single statements (feel free to express your outrage.) I think that I can get around

in-place vector construction from initialization list (for class with constructor arguments) [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-18 22:34:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Can I list-initialize a vector of move-only type? Edit 1: Please consider a re-open vote: My question emphasize in-place construction. Move construction is an alternative but not what this questions is about. Thanks for the answers! Edit 2: Since I can't answer this question (It got closed) I post my own suggestion here. The following is not as good as the answers I accepted, but may be useful for others. At

in-place vector construction from initialization list (for class with constructor arguments) [duplicate]

可紊 提交于 2019-12-18 22:33:22
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Can I list-initialize a vector of move-only type? Edit 1: Please consider a re-open vote: My question emphasize in-place construction. Move construction is an alternative but not what this questions is about. Thanks for the answers! Edit 2: Since I can't answer this question (It got closed) I post my own suggestion here. The following is not as good as the answers I accepted, but may be useful for others. At

Why can I use initializer lists on the right-hand side of operator += but not operator+?

荒凉一梦 提交于 2019-12-18 18:39:54
问题 This is a follow-up to an earlier question about why I can't use a brace-enclosed initializer as an argument to operator+, which was resolved by looking at this earlier question on the subject. Consider the following C++ code, which you can try live at ideone.com: #include <iostream> #include <initializer_list> using namespace std; struct AddInitializerList { void operator+= (initializer_list<int> values) { // Do nothing } void operator+ (initializer_list<int> values) { // Do nothing } }; int

Is it good habit to always initialize objects with {}?

爱⌒轻易说出口 提交于 2019-12-18 08:58:33
问题 Initializing objects with new {} syntax like this: int a { 123 }; has benefit - you would not declare a function instead of creating a variable by mistake. I even heard that it should be habit to always do that. But see what could happen: // I want to create vector with 5 ones in it: std::vector<int> vi{ 5, 1 }; // ups we have vector with 5 and 1. Is this good habit then? Is there a way to avoid such problems? 回答1: Frankly, the subtleties of the various initialization techniques make it

Having trouble passing multiple initializer lists to variadic function template

こ雲淡風輕ζ 提交于 2019-12-18 07:39:04
问题 I don't understand the error message when trying to pass a variable number of initializer lists: template<typename... Values> void foo(Values...) { } int main() { foo(1, 2, 3, "hello", 'a'); // OK foo({1}, {2, 3}); // ERROR } The error message complains about too many arguments: prog.cpp: In function ‘int main()’: prog.cpp:9:20: error: too many arguments to function ‘void foo(Values ...) [with Values = {}]’ foo({1}, {2, 3}); ^ prog.cpp:2:6: note: declared here void foo(Values...) ^ However,