initializer-list

are there any plans in C++ standard to address inconsistency of initializer list constructors?

北城余情 提交于 2019-12-09 14:52:35
问题 initializer list constructors in C++ often cause trouble; for example using std::vector; using std::string; vector<string> v{3}; // vector of three empty strings vector<int> u{3}; // vector of one element with value 3 (Just to clarify, I mean <int> constructor is an initializer list constructor, while the <string> one is not .) The int case matches the initializer list constructor, while the string case doesn't. This is somewhat ugly, and often causes trouble. It was also noted in an early

Possible MSVC 2013 bug when initializing struct members

大兔子大兔子 提交于 2019-12-09 12:57:46
问题 MSVC 2013 complains about the following code, while it works as expected in g++. Does this look like a bug in MSVC? #include <iostream> using namespace std; struct A { double x = 0.0, y = 0.0; }; int main() { A a{ 1.0, 2.0 }; return 0; } Note that changing the struct as follows resolves the issue. struct A { double x, y; }; The error message is: Error 1 error C2440: 'initializing' : cannot convert from 'initializer-list' to 'A' 回答1: Actually, Visual Studio is correct. Your class is not an

get the size of `std::initializer_list` at compile time

青春壹個敷衍的年華 提交于 2019-12-09 07:16:31
问题 I am trying to implement the reshape function of fortran with C++(11/14) and I designed a function. This function accepts two std::initializer_list . The first initializer_list gives initial values which I use to initialize a D -dimensional array. The second initializer_list gives size of each dimension of the array. I wrote a draft like this template<int D, typename T> auto forreshape(const std::initializer_list<T> & values, const std::initializer_list<int> & shape) { // some irrelevant

Initializer list of variables

旧街凉风 提交于 2019-12-09 03:47:58
问题 Is it possible to create an initializer_list of variables, like function arguments for example (cf. function test )? The code below works, and neither Clang nor GCC complain about anything, but I would just like to make sure this is OK. #include <iostream> #include <initializer_list> template <class T> struct array { T *ptr; size_t len; array() { clear(); } array( T *p, size_t l ) { assign(p,l); } inline void clear() { ptr=nullptr; len=0; } inline void assign( T *p, size_t l ) { ptr=p; len=l;

std::initializer_list, braced initialization and header

Deadly 提交于 2019-12-08 14:56:41
问题 While reading about a different topic I came across a weird behaviour, at least to me. This whole thought originated from the special interactions between auto and braces. If you write something like: auto A = { 1, 2, 3 } the compiler will deduce A to be a std::initializer_list . The weird thing is that a similar rule applies not only to auto , where there can be special reasons for it, but also to other things. If you write the following: template<typename T> void f(std::vector<T> Vector) {

call of overloaded <brace-enclosed initializer list> is ambiguous, how to deal with that?

旧时模样 提交于 2019-12-08 14:37:54
问题 I really don't understand this, I thought that compiler first executes what is in braces and then gives the result to the most appropriate function. Here it looks like it gives the function an initializer list to deal with it... #include <string> #include <vector> using namespace std; void func(vector<string> v) { } void func(vector<wstring> v) { } int main() { func({"apple", "banana"}); } error: <stdin>: In function 'int main()': <stdin>:11:27: error: call of overloaded 'func(<brace-enclosed

C++11 initializer_list constructor with header and cpp file for custom vector class

女生的网名这么多〃 提交于 2019-12-08 04:46:42
问题 I'm doing a school project where I am to construct a custom vector class. And the class should be able to initialize vectors in a few different ways. I've got stuck with this initializer_list initialization of the vector. The only values wich are allowed as elements are unsigned int. header #include <initializer_list> class myvec { private: unsigned int *arr; //pointer to array std::size_t n; //size of myvec public: myvec(); // Default contructor myvec(std::size_t size); // Creating a vec

Is this unsafe usage of a braced initializer list?

被刻印的时光 ゝ 提交于 2019-12-08 03:08:07
问题 I had a bug crop up in my program recently that surprised me a bit, but perhaps it shouldn't, with the vast number of types of initialization that C++ (especially modern C++) provides. I have a class that looks something like this: struct foo { foo(const std::set<int> &values) { values_ = values; } set::set<int> values_; }; When constructing foo s, it's usually easiest to specify the set of values inline using an initializer list (an instance will typically have 2-3 known values that are

C++11 initializer_list constructor with header and cpp file for custom vector class

痴心易碎 提交于 2019-12-08 02:18:26
I'm doing a school project where I am to construct a custom vector class. And the class should be able to initialize vectors in a few different ways. I've got stuck with this initializer_list initialization of the vector. The only values wich are allowed as elements are unsigned int. header #include <initializer_list> class myvec { private: unsigned int *arr; //pointer to array std::size_t n; //size of myvec public: myvec(); // Default contructor myvec(std::size_t size); // Creating a vec with # of element as size myvec(const myvec&); // Copy constructor myvec(const initializer_list<unsigned

Initializing private std::array member in the constructor

好久不见. 提交于 2019-12-07 10:20:27
问题 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