c++ - Data member and rvalue life-time -


somehow inspired expression template code in expression templates , c++11, written paul preney, decided test following:

template<typename t> struct x {     x(t t) : t(std::forward<t>(t)) {}      t t; };  template<typename t> auto createx(t&& t) -> x<decltype(std::forward<t>(t))> {     return x<decltype(std::forward<t>(t))>(std::forward<t>(t)); } 

then, used produce instance of x<const vector<int>&> , x<vector<int>&&> follows:

int main() {     int vec = {1,2,3,4};      auto x1 = createx(vec);     auto x2 = createx(vector<int>{5,6,7,8});      cout << "x1: "; for(auto x : x1.t) cout << x << " "; cout << endl;     cout << "x2: "; for(auto x : x2.t) cout << x << " "; cout << endl; } 

the output is:

x1: 1 2 3 4  x2: 0 0 33 0 0 0 7 8  

which shows life-time of temporary vector<int>{5,6,7,8} not being extended, , rvalue-reference member x::t binds else.

okay, answer what lifetime of class data member const reference rvalue?, know expected behaviour.

however, question here is: different in paul preney' code in expression templates , c++11 permits temporary vectors exist long rvalue-references members exist? see case 2, temporaries created.

apparently, same construct there used here, missing something.


edit: based on answer of r. martinho fernandes below, tried following:

int main() {     using namespace std;      auto expr = math_vector<3>{1.0, 1.1, 1.2} + math_vector<3>{2.0, 2.1, 2.2};      cout << "vec1: "; for(int = 0; < 3; ++i) cout << expr.le()[i] << " "; cout << endl;     cout << "vec2: "; for(int = 0; < 3; ++i) cout << expr.re()[i] << " "; cout << endl; } 

and turns out valid code outputs:

vec1: 1.0 1.1 1.2 vec2: 2.0 2.1 2.2 

therefore, apparently references stored in expression template not dangling. going on here?

what different in paul preney' code in expression templates , c++11 permits temporary vectors exist long rvalue-references members exist?

nothing permits such thing.

the temporary vectors there exist until end of full expression, other temporary isn't bound local reference variable. enough in paul's code because code materialises expression tree actual math_vector, , temporaries not needed anymore after that.

paul's code not store expression template node (math_vector_expr) anywhere, while code stores 1 (x) x2. that's known issue auto: wrong thing when using expression templates because results in storing expression tree, contain references become dangling right away.

in order make totally clear, following fine.

math_vector<3> result =     math_vector<3>{1.0, 1.1, 1.2} +     math_vector<3>{2.0, 2.1, 2.2} +     math_vector<3>{3.0, 3.1, 3.2} +     math_vector<3>{4.0, 4.1, 4.2} ; // no references held temporaries past point 

the following not fine.

math_vector_expr<3> result =        // or auto     math_vector<3>{1.0, 1.1, 1.2} +     math_vector<3>{2.0, 2.1, 2.2} +     math_vector<3>{3.0, 3.1, 3.2} +     math_vector<3>{4.0, 4.1, 4.2} ; // result holds references temporaries 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -