initializer-list

Can I initialize an STL vector with 10 of the same integer in an initializer list?

。_饼干妹妹 提交于 2020-07-04 06:07:50
问题 Can I initialize an STL vector with 10 of the same integer in an initializer list? My attempts so far have failed me. 回答1: I think you mean this: struct test { std::vector<int> v; test(int value) : v( 100, value ) {} }; 回答2: Use the appropriate constructor, which takes a size and a default value. int number_of_elements = 10; int default_value = 1; std::vector<int> vec(number_of_elements, default_value); 回答3: If you're using C++11 and on GCC, you could do this: vector<int> myVec () {[0 ... 99]

Initializer List for Derived Class

拥有回忆 提交于 2020-03-20 05:57:42
问题 I want to have a derived class which has a default constructor that initializes the inheirited members. Why can I do this class base{ protected: int data; }; class derived: public base{ public: derived(){ //note data = 42; } }; int main(){ derived d(); } But not this class base{ protected: int data; }; class derived: public base{ public: derived(): //note data(42){} }; int main(){ derived d(); } error: class ‘derived’ does not have any field named ‘data’ 回答1: An object can only be initialized

Implementation of std::initializer_list

独自空忆成欢 提交于 2020-03-17 10:03:10
问题 I have been looking at how the initializer_list is implemented so I found section 18.9 of the standard and found a simple enough looking interface. I thought it would be instructive to make my own version which I named MyNamespace::InitializerList and a use case: template<class T> class ArrayPrinter { public: ArrayPrinter(MyNamespace::InitializerList<T> list) { for (auto i : list) cout << i << endl; } }; ... ArrayPrinter ap{ {1,2,3} }; I was surprised to find that this did not work and the

Forms of list initialization

只愿长相守 提交于 2020-02-04 03:22:22
问题 See the following code: std::vector<int> v1{1, 2, 3}; std::vector<int> v2 = {1, 2, 3}; My questions are: Is there a difference between the two? I know the first one must be list initialization, but how about the second? Because there is a assign sign for the second, it makes me think that the compiler will use the std::initializer_list to create a temporary vector first, then it use copy constructor to copy the temp vector to v2 . Is this the fact? 回答1: The two (direct-list-initialization vs

C++11 is it possible to construct an std::initializer_list?

不羁岁月 提交于 2020-01-15 12:05:26
问题 I have a class that's using an std::discrete_distribution which can take an std::initializer_list OR a couple of iterators. My class is in some ways wrapping the discrete_distribution so I really wanted to mimic the ability to take an std::initializer_list which would then be passed down. This is simple. However, the std::initializer_list will always be constructed through some unknown values. So, if it was just a std::discrete_distribution I would just construct from iterators of some

Yet another C++ Object initialization interrogation

梦想的初衷 提交于 2020-01-15 10:18:53
问题 I have this class that has many class members, and a lot of different constructors. Until now, I used a constructor initialization list in each of the constructors that I have, tuning each member the way I wanted. This is quite tedious, because everytime I add a new member to my class, I have to visit each constructor and update the initialization list to add a default value to this member. So, I thought I would add a method to initialize the values I need. Problem ! Since the method is

c++ data members initialization order when using initialization list

我的未来我决定 提交于 2020-01-14 13:04:31
问题 class A { private: int a; int b; int c; public: A() : b(2), a(1), c (3) { } }; As per C++ standard data members are constructed and initialized in the order they are declared, correct? But when using initalization list, we are changing the order of data members, now do they initialize in order of initialization list or the order of declaration? 回答1: In the order of declaration, the order in the initialization list does not matter. Some compilers will actually give you warning (gcc) telling

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

痴心易碎 提交于 2020-01-14 08:54:08
问题 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:

Is it possible to avoid static_cast in initializer list?

你说的曾经没有我的故事 提交于 2020-01-13 10:05:38
问题 In my code base I often initialize array or vector if bytes using the following the syntax: uint16_t foo = 0xAB, bar = 0xCD // bytes = { 0xA, 0xB, 0xC, 0xD } std::array<uint8_t, 4> bytes = {{ foo >> 8, foo & 0x00FF, bar >> 8, bar & 0x00FF }}; I get the following error from clang++: error: non-constant-expression cannot be narrowed from type 'int' to 'value_type' (aka 'unsigned char') in initializer list [-Wc++11-narrowing] foo >> 8, ^~~~~~~~~~~~~ The compiler suggest me to add a static_cast