So I have been reading about std::move, std::forward, rvalues, lvalues ad so on in SO and other places. But I find that I can’t grasp it. Even though I sometimes get into fixes, I think I understand basic stuff about pointers, references, etc which were in C++ before all this. Is it me or are these stuff getting too heavy?
So I have been reading about std::move , std::forward , rvalues, lvalues ad so
Share
Your question is very general. Maybe I can get you started:
std:move()andstd::forward()at the beginningMatrix z = a + b + c + d;(withMatrix a,b,c,d;)operator+onMatrixwith overloaded RValue References.If you want to see a simple use of
std::move(): Help the compiler to avoid introducing a copy for a return value:Image— costly to copy.invent a factory function that works like this:
Image load_matching_size(const char *fn_small, const char *fn_big) { pair<Image> ii = load_2_images(fn_small, fn_big); return ii.first.width() >= 64 ? ii.first : ii.second; }Can you count the numbers of temporaries? Note that the
returnwill need an additional one and copy! (The example is designed so that return-value-optimization (“RVO”) should not be possible)iiwill be thrown away shorty after the function returned. Could the compiler use then for the return value? (No it could not. RVO would work if we had only oneImage).movein thereturnyou can tell the compiler, that you do not neediifurther and that it can use it for the return. And thus spare a costly copy my using the move-c’tor instead of the copy-c’tor for the return.