member-initialization

How do I forward the values of tuple to a member initializer?

半城伤御伤魂 提交于 2021-01-27 16:28:04
问题 I need to forward the values of a tuple to a member initializer: struct Struct { Member1 member1; Member2 member2; template<typename Tuple1, typename Tuple2> Struct( Tuple1&& tuple1, Tuple2&& tuple2 ) : member1(tuple1...), member2(tuple2...) {} }; The code above obviously isn't valid. How can I express it? Member1 and Member2 have no default/copy/move constructor. I know about std::apply , as suggested in How do I expand a tuple into variadic template function's arguments?. I also know about

Order of member initializers [duplicate]

时光总嘲笑我的痴心妄想 提交于 2020-02-21 13:51:52
问题 This question already has answers here : Initialization Order of Class Data Members (2 answers) Closed 2 years ago . Following code gives correct output, If I declare variables i and j , Like int i, j; class A { int i, j; public: A(int val) : i(val), j(i + 1) { cout<<i<<endl<<j<<endl; } }; But If I declare variable i and j , like int j, i; . then j print garbage value . class A { int j, i; public: A(int val) : i(val), j(i + 1) { cout<<i<<endl<<j<<endl; } }; So, Is it depend on order of

Order of member initializers [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-21 13:48:11
问题 This question already has answers here : Initialization Order of Class Data Members (2 answers) Closed 2 years ago . Following code gives correct output, If I declare variables i and j , Like int i, j; class A { int i, j; public: A(int val) : i(val), j(i + 1) { cout<<i<<endl<<j<<endl; } }; But If I declare variable i and j , like int j, i; . then j print garbage value . class A { int j, i; public: A(int val) : i(val), j(i + 1) { cout<<i<<endl<<j<<endl; } }; So, Is it depend on order of

Is it allowed to call a non-static member function in a default member initializer?

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-02 11:27:12
问题 Consider this class: #include <iostream> struct foo { int a = 42; int b = bar(); int bar() { return a; } }; int main(){ foo f; std::cout << f.a << " " << f.b; } It prints the expected 42 42 . Is it allowed by the standard to call a member function in a default member initializer? The following I would expect to be undefined: struct broken { int a = bar(); int b = 42; int bar() { return b; } }; Unfortunately it does compile without warnings. 回答1: As you found, this is legal, but brittle and

Initialization of member: bug in GCC or my thinking?

依然范特西╮ 提交于 2020-01-02 05:34:07
问题 I've got an enum type defined in the private section of my class. I have a member of this type defined as well. When I try to initialize this member in the constructor body, I get memory corruption problems at run-time. When I initialize it through an initialization list in the same constructor instead, I do not get memory corruption problems. Am I doing something wrong? I'll simplify the code, and if it is a GCC bug I'm sure that it's a combination of the specific classes I'm combining

What is “member initializer” in C++11?

好久不见. 提交于 2019-12-30 17:06:30
问题 I run across a weird concept named "member initializer". Here says: C++11 added member initializers, expressions to be applied to members at class scope if a constructor did not initialize the member itself. What is its definition? Are there some examples to illustrate its usage? 回答1: It probably refers to in-class member initializers. This allows you to initialize non-static data members at the point of declaration: struct Foo { explicit Foo(int i) : i(i) {} // x is initialized to 3.1416 int

How to initialize multiple constant member variables that shares complex initialization code?

孤人 提交于 2019-12-24 14:13:53
问题 Introduction Let's introduce this simple example: #include <cmath> class X { public: // Members /// A ^ B + A int A; /// A ^ B + B int B; public: // Specials X( const int & A, const int & B ) : A(A) , B(B) { const auto Pow = static_cast<int>(std::pow(A, B)); this->A += Pow; this->B += Pow; } }; Trivia Introduced class has two member variables: A and B . They take value of A ^ B + A and A ^ B + B , respectively. Both of them shares common complex initialization code (let's assume std::pow is

Order of member initialization list [duplicate]

扶醉桌前 提交于 2019-12-23 05:40:06
问题 This question already has answers here : C++: Initialization Order of Class Data Members (2 answers) Closed last year . After simplifying my code for many times, I found the following cause the problem. class B { public: B(const int x) :_x(x) {} const int _x; }; class C { public: C(const B& b) : _b(b), _b2(_b._x) {} B _b2; // line 1 const B& _b; // line 2 }; int main() { B b(1); C c(b); } Warning (clang 8.0.0) test16.cpp:11:22: warning: reference '_b' is not yet bound to a value when used

Illegal Member initialization

无人久伴 提交于 2019-12-21 16:18:11
问题 I am using this pretty simple class without using any inheritance. class A { int a; int b; public: A(int x, int y) { a = x; b = y;} A() :A(0,0){}; ~A(){}; } ; int main () { A a1, a2(5, 7) ; } I get this error. error C2614: 'A' : illegal member initialization: 'A' is not a base or member There are similar questions on SO but they relate to inheritance. Can someone explain the reason and what does standard say about that? EDIT: It would be better if someone elaborate more on the forwarding

Should trivial default constructor respect default member initializer here?

邮差的信 提交于 2019-12-07 06:40:27
问题 Consider the code: #include <atomic> #include <iostream> struct stru { int a{}; int b{}; }; int main() { std::atomic<stru> as; auto s = as.load(); std::cout << s.a << ' ' << s.b << std::endl; } Note that although stru has default member initializer, it still qualifies as an aggregate type since C++14. std::atomic has a trivial default constructor. According to the standard, should the members of as be initialized to zero? clang 6.0.0 doesn't do this (see here), while gcc 7.2.0 seems so (see