initializer-list

Is it direct-initialization or copy-initialization?

二次信任 提交于 2019-12-18 05:24:07
问题 Initializing objects (instances of classes or structs) in C++ can be done in various ways. Some syntaxes evoke a direct-initialization of your object, other syntaxes lead to a copy-initialization . With copy-elision enabled in the compiler, both have identical performance. With copy-elision disabled, there is an additional copy/move constructor call upon every instantiation when you choose for the latter (copy-initialization). Conclusion: copy-initialization can have a performance-penalty!

std::shared_ptr in an std::initializer_list appears to be getting destroyed prematurely

一个人想着一个人 提交于 2019-12-18 05:15:33
问题 Edit: This is indeed caused by a bug in Visual Studio - and it has already been fixed. The issue is not reproducible after applying Update 2 to Visual Studio (release candidate available here). I apologize; I thought I was up to date with my patches. I can't for the life of me figure out why I get a seg fault when I run the following code in Visual Studio 2013: #include <initializer_list> #include <memory> struct Base { virtual int GetValue() { return 0; } }; struct Derived1 : public Base {

Default values in C++ initializer lists

筅森魡賤 提交于 2019-12-17 22:42:32
问题 I only just learned yesterday that specifying parameters to initializer list items is optional. However, what are the rules for what happens in this case? In the below example, will ptr be initialized to 0, toggle to false, and Bar default-constructed? I guess this question is sort of redundant, because there would be little point in initializer lists if unspecified argument values == undefined behavior. Could I also be pointed to the section of the C++ standard that states the behavior in

Initializer-list-constructing a vector of noncopyable (but movable) objects

送分小仙女□ 提交于 2019-12-17 19:57:12
问题 One can push_back rvalues of a noncopyable-but-movable type into a vector of that type: #include <vector> struct S { S(int); S(S&&); }; int main() { std::vector<S> v; v.push_back(S(1)); v.push_back(S(2)); v.push_back(S(3)); } However, when I try to initializer-list-construct the vector with the same rvalues, I get errors about a copy constructor being required: #include <vector> struct S { S(int); S(S&&); }; int main() { std::vector<S> v = {S(1), S(2), S(3)}; } I get the following errors with

Templates don't always guess initializer list types

假装没事ソ 提交于 2019-12-17 15:46:45
问题 #include <initializer_list> #include <utility> void foo(std::initializer_list<std::pair<int,int>>) {} template <class T> void bar(T) {} int main() { foo({{0,1}}); //This works foo({{0,1},{1,2}}); //This works bar({{0,1}}); //This warns bar({{0,1},{1,2}}); //This fails bar(std::initializer_list<std::pair<int,int>>({{0,1},{1,2}})); //This works } This doesn't compile in gcc 4.5.3, it gives a warning for the marked line saying deducing ‘T’ as ‘std::initializer_list<std::initializer_list<int> >’

Double delete in initializer_list vs 2013

你。 提交于 2019-12-17 06:54:15
问题 Today in run into a memory problem in my project, with a class using c++ 11 initializer_list. The system signals a memory problem: "Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) in dbgdel.cpp. I simplified the code to a simple example, it no longer throws an expression but the problem becomes apparent from the debug output. In my eyes this code is correct, also it seems to work with g++. #include <functional> #include <memory> #include <string> #include <iostream> #include <vector>

Double delete in initializer_list vs 2013

馋奶兔 提交于 2019-12-17 06:54:00
问题 Today in run into a memory problem in my project, with a class using c++ 11 initializer_list. The system signals a memory problem: "Expression _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) in dbgdel.cpp. I simplified the code to a simple example, it no longer throws an expression but the problem becomes apparent from the debug output. In my eyes this code is correct, also it seems to work with g++. #include <functional> #include <memory> #include <string> #include <iostream> #include <vector>

Why doesn't my template accept an initializer list

爱⌒轻易说出口 提交于 2019-12-17 05:07:34
问题 I have created a template as follows template<typename T> void f(T const& t) { } I wanted for this to be callable by containers but also by initializer lists. I thought it would be initializer_list<int> , when called as follows. f({1, 2, 3}); But GCC behaves as if it's not Standards compliant m.cpp: In function 'int main()': m.cpp:6:25: warning: deducing 'const T' as 'const std::initializer_list<int>' m.cpp:4:6: warning: in call to 'void f(const T&) [with T = std::initializer_list<int>]' m

Is legal use initializer_list to initialize an object with derived types?

家住魔仙堡 提交于 2019-12-14 04:08:49
问题 Well, maybe from the title is not clear what I'm actually asking. I have a class with an initializer-list constructor std::initializer_list<B> . Is legal initialize it with an initializer list of objects of class D , where D is derived from B ? #include <initializer_list> struct B { B(int) {} }; struct D: public B { D(int s): B(s) {} }; struct Foo { Foo(std::initializer_list<B> l) {} }; void main() { Foo f{ D{ 1 }, D{ 2 } }; } If is not legal, is that ill-formed? or just undefined behavior? I

Cannot open include file: 'initializer_list'

一世执手 提交于 2019-12-14 02:17:44
问题 I want to compile The Forgotten Server (Tibia OTServer) on Microsoft Visual Studio 2012 (MSVC). But at the file: unordered_set.hpp I have this: #if !defined(BOOST_NO_0X_HDR_INITIALIZER_LIST) #include <initializer_list> #endif and is giving me the errors: boost/unordered/unordered_set.hpp(27): fatal error C1083: Cannot open include file: 'initializer_list': No such file or directory (..\spells.cpp) boost/unordered/unordered_set.hpp(27): fatal error C1083: Cannot open include file: 'initializer