initializer-list

Is it possible to avoid static_cast in initializer list?

两盒软妹~` 提交于 2020-01-13 10:05:10
问题 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

Member initializer list notation: brackets vs parentheses

六眼飞鱼酱① 提交于 2020-01-11 08:45:12
问题 Consider the following code snippet from pg. 17 's A Tour of C++ : class Vector { public: Vector(int s) :elem{new double[s]}, sz{s} { } //construct a Vector double& operator[](int i) { return elem[i]; } //element access: subscripting int size() { return sz; } private: double* elem; // pointer to the elements int sz; // the number of elements }; Here I'm concerned about the member initializer list on the third line, where Stroustrup separates a colon from the two initializer statements elem

C++ overload constructor with const array and initializer_list

元气小坏坏 提交于 2020-01-07 03:41:09
问题 I have a class that takes a "list" of pins as argument in the constructor. Right now, this "list" is just a const array ( const unsigned int (&pins)[N] ). I'd like to overload the constructor in such a way that you can use an initializer_list as well as a (const) array. I can get it working with separate classes, but I'm unable to combine them. The basic structure of my test class: #include <iostream> #include <cstring> using namespace std; class X { public: X(... pins); ~X(); void print() {

How function call is working on an unitialized data member object in constructor's initilalizer list

不羁的心 提交于 2020-01-07 03:07:53
问题 Consider below program, #include <iostream> using namespace std; class A { public: A() { cout << "A constructor\n"; } void f() { cout << "A used\n"; this->i++; } private: int i; }; class B { public: B(A & a1) { cout << "B constructor\n"; a1.f(); } }; class Z { public: Z() : a_(), b_(a_) {} private: B b_; A a_; }; int main() { Z z; return 0; } Below is output it produces, B constructor A used A constructor My Question, Since data member objects are created in order of their declaration in

How function call is working on an unitialized data member object in constructor's initilalizer list

对着背影说爱祢 提交于 2020-01-07 03:07:08
问题 Consider below program, #include <iostream> using namespace std; class A { public: A() { cout << "A constructor\n"; } void f() { cout << "A used\n"; this->i++; } private: int i; }; class B { public: B(A & a1) { cout << "B constructor\n"; a1.f(); } }; class Z { public: Z() : a_(), b_(a_) {} private: B b_; A a_; }; int main() { Z z; return 0; } Below is output it produces, B constructor A used A constructor My Question, Since data member objects are created in order of their declaration in

std::initializer list from already existing std::array without enumerating each element

限于喜欢 提交于 2020-01-05 04:27:27
问题 Lets assume that I have a class with the following constructor: class Foo { Foo(std::initializer_list<uin8_t> args) { .... } } and I have the following array: std::array<uint8_t, 6> bar . Now I would like to create an object off foo with the bar array. Is there an other way as doing it in the follwing way: Foo f (bar[0], bar[1], bar[2], bar[3], bar[4], bar[5]); This way seems a bit complicated and it feels like this is not the way it should be. So, can I create a std::initializer list from an

What does initializer_list do?

丶灬走出姿态 提交于 2020-01-04 07:03:04
问题 I'm currently tasked with creating a definition for a special custom class my professor provided to us; I've never seen initializer_list in use, nor did my professor go over it. What exactly does it do? template<class T> LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>& il) { } I have to define the constructor, which I will do on my own. I just want to know what this parameter does. Also, my compiler keeps telling me that std::initializer_list member declaration is not found. I've

What does initializer_list do?

梦想与她 提交于 2020-01-04 07:02:15
问题 I'm currently tasked with creating a definition for a special custom class my professor provided to us; I've never seen initializer_list in use, nor did my professor go over it. What exactly does it do? template<class T> LinkedQueue<T>::LinkedQueue(const std::initializer_list<T>& il) { } I have to define the constructor, which I will do on my own. I just want to know what this parameter does. Also, my compiler keeps telling me that std::initializer_list member declaration is not found. I've

Type of an auto initialized list

僤鯓⒐⒋嵵緔 提交于 2020-01-04 06:27:09
问题 In the C++ code below, what is type of a ? typeid returns St16initializer_listIPKcE auto a = { "lol", "life" }; 回答1: When you have auto a = { "lol", "life" }; The compiler will try to deduce a std::initializer_list where the type is what all of the elements are. In this case "lol" and "life" are both a const char[] so you have a std::initializer_list<const char*> . If on the other have you had something like auto foo = { 1, 2.0 }; Then you would have a compiler error since the element types

C++17 almost uniform initialization

南笙酒味 提交于 2020-01-03 05:23:09
问题 At the end of this video (starting at 15:57) there is advice on how to use almost uniform initialization in C++17: video here The gist goes like this: use always direct initialization auto a{...}; and MyType a{...}; Do not use copy initialization = {...} for your types. #include <iostream> struct MyType { explicit MyType(std::initializer_list<int>) { std::cout << "Called std::initializer_list<int>" << std::endl; } explicit MyType(int) { std::cout << "Called int." << std::endl; } MyType(int,