list-initialization

int a[] { (functioncall(a1, a2), 0)…}; (void(a)); What does this syntax do/mean?

霸气de小男生 提交于 2019-12-12 09:57:32
问题 I came across this post variadic template function to concatenate std::vector containers suggesting the use of the following syntax: template<typename T> void append_to_vector(std::vector<T>& v1, const std::vector<T>& v2) { std::cout << v2[0] << std::endl; for (auto& e : v2) v1.push_back(e); } template<typename T, typename... A> std::vector<T> concat_version3(std::vector<T> v1, const A&... vr) { int unpack[] { (append_to_vector(v1, vr), 1)... }; (void(unpack)); return v1; } I started playing

Why is a braced-init-list not an expression?

a 夏天 提交于 2019-12-11 00:28:38
问题 While I am reading page93 $5.1.2 of the C++11 standard, during which it said it is ellegal for you to use the braced-init-list in this case: auto x=[]{return {1,2}}; //error: a braced-init-list is not an expression And I have found these two topics, one from the standard and the other from N3681 proposal. Page397 $14.8.2.5:an initializer list argument causes the parameter to be considered a non-deduced context. and $7.6.1.4:replacing the occurrences of auto with either a new invented type

Narrowing Conversion required while list initialization

旧巷老猫 提交于 2019-12-10 20:39:41
问题 I read about narrowing conversion on the cpp reference website. I kind of understood it but what i am not getting is that why is the error present only in the first line. long double ld = 3.1415926536; int a{ld}, b = {ld}; // error: narrowing conversion required int c(ld), d = ld; // ok: but value will be truncated Why is the error only present in first line and not the second? 回答1: Because the compiler is required to issue a diagnostic (in your case error) for narrowing only for list

C++11 empty list Initialization of a union - is it guaranteed to initialize the full length of the union?

倾然丶 夕夏残阳落幕 提交于 2019-12-10 12:49:02
问题 In C++11, I have the following union: union SomeData { std::uint8_t Byte; std::uint16_t Word; std::uint32_t DWord; unsigned char String[128]; }; If I initialize the union thusly; SomeData data {}; Is it guaranteed that the entire contents of the union will be "zero'd" out? Put another way; is an empty list-initializer of a union functionally equivalent to memset-ing the union to Zero?: memset(&data, 0, sizeof(data)); In particular, I'm concerned about the string data. I'd like to ensure the

Is this unsafe usage of a braced initializer list in a range-based for loop?

旧时模样 提交于 2019-12-10 03:14:58
问题 This is very similar to a question I asked earlier today. However, the example I cited in that question was incorrect; by mistake, I was looking at the wrong source file, not the one that actually had the error I described. Anyway, here's an example of my problem: struct base { }; struct child1 : base { }; struct child2 : base { }; child1 *c1; child2 *c2; // Goal: iterate over a few derived class pointers using a range-based for loop. // initializer_lists are convenient for this, but we can't

Why does direct list initialization causes ambiguity for type reference cast if cast operators to the type and reference to the type are declared?

孤街醉人 提交于 2019-12-10 02:02:24
问题 The question rose in context of this answer. Consider an example: struct foo { int value; operator int&(){ return value; } operator int(){ return value; } }; int main () { int &a(foo{}); // #1 //int &b{foo{}}; // #2 -- ambiguity int &c = foo{}; // #3 //int &d = {foo{}}; // #4-- ambiguity int &d { a }; // #5 int &e = { a }; // #6 (void)a; (void)c; (void)d; (void)e; } I don't understand why does #2 and #4 cause ambiguity while #1 and #3 does not. So the question is - why does direct list

Brace initialization subtleties

痴心易碎 提交于 2019-12-08 03:23:37
问题 While trying to use brace initialization , a subtlety that can be found is when using std::vector , like showed in the following sample: #include <iostream> #include <string> #include <vector> using namespace std; template <typename T> void print(const char * msg, const vector<T>& v) { cout << msg << endl; cout << "Size: " << v.size() << endl; for (size_t i = 0; i < v.size(); ++i) { cout << "#" << (i+1) << ": " << v[i] << endl; } cout << "---------------------" << endl; } int main() { vector

Brace initialization subtleties

放肆的年华 提交于 2019-12-06 13:57:14
While trying to use brace initialization , a subtlety that can be found is when using std::vector , like showed in the following sample: #include <iostream> #include <string> #include <vector> using namespace std; template <typename T> void print(const char * msg, const vector<T>& v) { cout << msg << endl; cout << "Size: " << v.size() << endl; for (size_t i = 0; i < v.size(); ++i) { cout << "#" << (i+1) << ": " << v[i] << endl; } cout << "---------------------" << endl; } int main() { vector<string> vs{3}; print("vector<string> vs{3};", vs); vector<int> vi{3}; print("vector<int> vi{3};", vi);

int a[] { (functioncall(a1, a2), 0)…}; (void(a)); What does this syntax do/mean?

喜欢而已 提交于 2019-12-06 07:28:30
I came across this post variadic template function to concatenate std::vector containers suggesting the use of the following syntax: template<typename T> void append_to_vector(std::vector<T>& v1, const std::vector<T>& v2) { std::cout << v2[0] << std::endl; for (auto& e : v2) v1.push_back(e); } template<typename T, typename... A> std::vector<T> concat_version3(std::vector<T> v1, const A&... vr) { int unpack[] { (append_to_vector(v1, vr), 1)... }; (void(unpack)); return v1; } I started playing around with it to understand how it worked, since I haven't seen this: int unpack[] { (append_to_vector

Braced-init-lists and function template type deduction order

杀马特。学长 韩版系。学妹 提交于 2019-12-06 01:32:53
问题 I have a question regarding the function template parameter type deduction procedure. Take this example: #include <vector> #include <sstream> #include <string> #include <iterator> #include <fstream> int main() { std::ifstream file("path/to/file"); std::vector<int> vec(std::istream_iterator<int>{file},{}); // <- This part return 0; } If I understand things correctly, the second parameter is deduced to be of type std::istream_iterator of which the default constructor is called. The appropriate