initializer-list

Is it possible to initialise an array of non-POD with operator new and initialiser syntax?

折月煮酒 提交于 2019-12-29 07:34:07
问题 I have just read and understood Is it possible to initialise an array in C++ 11 by using new operator, but it does not quite solve my problem. This code gives me a compile error in Clang: struct A { A(int first, int second) {} }; void myFunc() { new A[1] {{1, 2}}; } I expected {{1, 2}} to initialise the array with a single element, in turn initialised with the constructor args {1, 2}, but I get this error: error: no matching constructor for initialization of 'A' new A[1] {{1, 2}}; ^ note:

How to write a wrapper for std::array's list initialization constructor?

五迷三道 提交于 2019-12-25 08:41:11
问题 I’m writing a wrapper that, for the purpose of this question, does nothing but wraps a SequenceContainer ( http://en.cppreference.com/w/cpp/concept/SequenceContainer ) and reproduces all functionality of the SequenceContainer concept the wrapped container offers. My attempt to write such a wrapper looks like this: template<class Container> class SequenceContainerWrapper { Container cont; public: using value_type = typename Container::value_type; using reference = typename Container::reference

Initializer list not working in Visual Studio 2012 Update 2 CTP 4 (March)

人走茶凉 提交于 2019-12-24 20:28:35
问题 After installing Visual Studio 2012 Update 2 CTP 4 (March), this code doesn't compile: vector<int> b = {1, 2, 3}; with the following error message: 'std::vector<_Ty>' : Types with a base are not aggregate Earlier post about the same issue with previous CTP mentioned using initializer_list header, but CTP 4 didn't install it. Any suggestions how to fix it? 回答1: The compiler supports initializer lists, but the standard library (std::vector etc.) does not. You will have to wait before your line

Using parent class constructor initialiser-list in C++

泄露秘密 提交于 2019-12-24 18:30:09
问题 ORIGINAL POST When compiled, the following code produces error C2436: '__ctor' : member function or nested class in constructor initializer list in Child.h #include Parent.h class Child : public Parent { public: Child (List* pList) : Parent::Parent(pList) { } }; Here the parent class: in Parent.h class __declspec(dllimport) Parent : public GrandParent { public: Parent (List* pList = NULL); } in Parent.cpp Parent::Parent (List* pList) : m_a(1) ,m_b(1) ,GrandParent(pList) { } Isn't it right the

Visual Studio 2012 Update 3 - initializer list & variadic templates

一世执手 提交于 2019-12-24 12:13:42
问题 Recently I have installed Visual Studio 2012. After the installation I updated my IDE with update 3 to guarantee functionality of my programs on Windows XP. Everything is working well, but I still can not use initializer list and variadic templates! Do I need any extra updates to get this working with Visual Studio 2012? 回答1: VS2012 does not support variadic templates and initializer lists, even with the latest updates. VS2013 RC, however, supports both. For a full overview of what C++11

Why can't I initialize a value using braces with auto and pass it into this function

南楼画角 提交于 2019-12-24 11:30:04
问题 Why can't I initialize a value with auto and pass it into a function that expects a decltype as a parameter? Let me set the scene, and show you a tiny program. Here is a function that returns a value. int Function(void); In this case, it happens to be an integer, but the return type is subject to change. That is why this next function, is written as followed: void What_I_Take_Depends_On_Function(decltype(Function()) x); If someone decides to change the return type of Function, then the

Performance issues when initializing each class member?

旧街凉风 提交于 2019-12-24 10:23:27
问题 I consider this SO question as a general point of view regarding the initialization and it answers already a lot: Should constructor initialize all the data members of the class? But, I didn't find really anything regarding a possible performance issue when for example my class has 100 members and I initialize each member with the {} command, just because Eclipse is warning me about uninitialized member: Member 'foo' was not initialized in this constructor My question: Can an initialization

Initializer list for an unknown (“templated”) amount of classes

时光毁灭记忆、已成空白 提交于 2019-12-24 08:33:51
问题 If I have a class template which contains an array with another class as type with undefined amount of fields (the amount is a template parameter), how do I run their constructors (if they take parameters)? Here some example code: class ArrayClass { public: ArrayClass() = delete; ArrayClass(int anyParameter) {} }; template <const int amountOfFields> class ContainingClass { ArrayClass myArray[amountOfFields]; public: ContainingClass(); }; template <const int amountOfFields> ContainingClass

variable number of arguments, same specific type without macro or initializer-list

时光怂恿深爱的人放手 提交于 2019-12-24 07:25:15
问题 I'm trying to do something similar to C++11 variable number of arguments, same specific type, but I have my own type: struct Foo { Foo(int) {} Foo(int, int) {} }; with a bunch of overloads void f() {} void f(const Foo&) {} void f(const Foo&, const Foo&) {} // etc. ... as many f() overloads as needed ... works as desired: f(); f(1); f(1, 2); f(1, { 2, 3 }); . Instead of overloads, I can also use std::initializer_list<> with the {} syntax (as suggested here): void g_(std::initializer_list<Foo>)

In C++ template copy assignment operator not compatible with initializer_list?

吃可爱长大的小学妹 提交于 2019-12-24 03:29:37
问题 Consider I have such code: #include <initializer_list> class my_class { public: my_class() {} void operator = (const std::initializer_list<int>&) {} // OK template<typename ValueType> void operator = (const ValueType&) {} // Failed }; int main(int argc, char* argv[]) { my_class instance; instance = {1, 2}; return 0; } The first copy assignment operator could be compiled OK with instance = {1, 2} . However, the template version would failed with such error: code.cpp:15:14: error: no viable