initializer-list

Crash in VS2013 SP5 when inserting initializer_list into vector<string>

蹲街弑〆低调 提交于 2019-12-10 18:26:04
问题 Seeing a crash with the below code. I know initialize_lists have a poor reputation in VS, but I thought most of them were fixed with 2013 sp3. The same code works fine in g++ (6.1). Am I missing something here? This seems far too simple. The debugger suggests a problem with an invalid iterator in the xstring module. The same code works find with integers, so I'm guessing it relates to some sort of string specific optimization. #include <vector> #include <iostream> #include <string> int main

Use initializer list to create single item vector

家住魔仙堡 提交于 2019-12-10 17:13:04
问题 I have a function func which is overloaded to take either a std::vector<Obj> argument or a Obj argument. #include <vector> #include <iostream> class Obj { int a = 6; }; void func(const std::vector<Obj>& a) { std::cout << "here" << std::endl; } void func(const Obj& a) { std::cout << "there" << std::endl; } int main() { Obj obj, obj2; func({obj}); func({obj, obj2}); } Actual output: there here Expected output: here here It seems {obj} doesn't initialize a vector, but rather an object. I guess

In C++, how can I create a `std::initializer_list<base *>` without new and without declaring individual elements separately?

主宰稳场 提交于 2019-12-10 17:07:15
问题 In C++, you can declare an array of something at the file scope: static foo a[] = { foo(), foo(), foo() }; The individual foo objects have static storage (i.e. they are not allocated at run-time). If I had a base class inherited by two or more derived classes, the following would compile but not work as expected due to slicing: static base a[] = { derived1(), derived2() }; Something like this should not cause slicing to happen: static derived1 d1; static derived2 d2; static base *a[] = { &d1,

why initializer list cannot be main's parameter? how to propose it?

大兔子大兔子 提交于 2019-12-10 14:49:01
问题 The valid C++ main signatures are the following: int main() int main(int argc, char *argv[]) int main(int argc, char **argv) But isn't allowed to declare main taking an initializer list: int main(std::initializer_list<char *> args) AFAIK the initializer list could be implemented as a pair of pointers or a pointer (this could be the argv parameter) plus a length (this could be deduced from the argc parameter), and its storage could be automatic, temporary, or static read-only memory depending

assignment operator on empty inizializer_list

让人想犯罪 __ 提交于 2019-12-10 14:41:19
问题 can you explain how STL containers handle assignment operator with empty initializer list? when i'll do something like this: vector<int> v; v = { }; the function that is called is not : vector& operator= (initializer_list<value_type> il); but: vector& operator= (vector&& x); on the other hand, when i'll do something similar with my own class: struct A { A& operator= (const A&) { return *this; } A& operator= (A&&) { return *this; } A& operator= (initializer_list<int>) { return *this; } }; /* .

Why must <initializer_list> be included for using auto?

白昼怎懂夜的黑 提交于 2019-12-10 14:34:05
问题 There has already been a similar question on SO, but I want to stress another aspect of braced-init-lists . Consider the following: auto x = {1}; //(1) This is ill-formed (8.5.4/2) unless the header <initializer_list> is included. But why? The standard says, that the template std::initializer_list is not predefined. Does this mean, that declaration (1) introduces a new type? In all other situations, where auto may be used such as auto y = expr; where expr is an expression, the type auto

Initializer lists and RHS of operators

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 13:14:48
问题 I do not understand why initializer lists cannot be used on the RHS of an operator. Consider: class foo { }; struct bar { template<typename... T> bar(T const&...) { } }; foo& operator<<(foo& f, bar const&) { return f; } int main() { foo baz; baz << {1, -2, "foo", 4, 5}; return 0; } The latest Clang (gcc as well) complains: clang.cc:14:9: error: initializer list cannot be used on the right hand side of operator '<<' baz << {1, -2, "foo", 4, 5}; ^ ~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~ Why

Innocent range based for loop not working

不想你离开。 提交于 2019-12-10 12:33:52
问题 The following does not compile: #include <iostream> int main() { int a{},b{},c{},d{}; for (auto& s : {a, b, c, d}) { s = 1; } std::cout << a << std::endl; return 0; } Try it on godbolt Compiler error is: error: assignment of read-only reference 's' Now in my actual case the list is made of member variables on a class. Now, this doesn't work because the expression becomes an initializer_list<int> that actually copies a,b,c, and d - hence also not allowing modification. My question is two-fold:

How to do nested initializer_lists in visual C++ 2013

别来无恙 提交于 2019-12-10 03:39:54
问题 I've got a program which works in g++ and clang, using a nested initializer_list. In Visual C++, the 1D case works, but a 2D nested initializer_list does not. Is there a trick to make Visual C++ work, or is this maybe a bug in their implementation? Here's my example code. It works in Visual C++ 2013 if I remove the annotated line. #include <iostream> #include <initializer_list> using namespace std; template<class T> void print(T val) { cout << val; } template<class T> void print(initializer

Could not convert from brace-enclosed initializer list to std tuple

匆匆过客 提交于 2019-12-10 03:28:58
问题 As part of a bigger project, I'm playing with std::tuple and templates; consider the following code: template <typename ...T> void foo(tuple<T...> t) {} void bar(tuple<int, char> t) {} tuple<int, char> quxx() { return {1, 'S'}; } int main(int argc, char const *argv[]) { foo({1, 'S'}); // error foo(make_tuple(1, 'S')); // ok bar({1, 'S'}); // ok quxx(); // ok return 0; } According to this answer C++17 supports tuple initialization from copy-list-initialization , however it seems such support