Should I try to use r-value links as often as possible? Here, for example, the code:
std::string hi() { return "hello world\n" } auto&& str = hi(); In this case, in line 5, only one object is created, and the r-value reference to it, and no copying ... effectively, if we compare, for example, with:
auto str = hi(); or
auto str = std::move(hi()); Therefore, at first glance, the conclusion is: why not create r-value links as often as possible instead of the usual initialization of a variable? Especially if you need to create 100,000 times per second these variables. Suppose, if possible, write so:
void function() { int&& a = 1; double&& b = 2.; auto&& value = return_value(); /*далее какой-то код*/ } continue to work with these links or transfer them to another place ... it looks like it is a bit more monstrous than the usual initialization of variables to which the eye is used:
void function() { int a = 1; double b = 2.; auto value = return_value(); /*далее какой-то код*/ } Therefore, is it worth using r-value links as often as possible instead of the usual initialization of new variables in a local context (inside functions, for example) for efficiency, or are there any built-in optimizations that, for int a = 2 do the same actions as int&& a = 2 ?
int&&a = 2;beforeint a = 2;? - Harryint&& a = 2at run time, a temporary object of typeintwill be formed and initialized to value 2. And this is the same asint a = 2. - AnT