Best practice for const temporary types
Why would you bind a reference to val * val
? Performance aside, I would consider this as obfuscation.
Unsurprinsgly, for this simple example, -O3 compiles down to the same code anyway:
Yes, no big surprise here. So just use the most concise and least contrived. const
is not for performance and the compiler is clever enough to realize that the variable isnt modified without your help, though const
documents the constness for you and thus adds to readability. Hence
const auto tmp = val * val;
Anything else is turning something simple into something unnecessarily complicated.
Last but not least, consider if you need the temporary at all, or if you can simply write val*val
in place of tmp
. On the other hand, if you use tmp
many times, it is of course worth to give it a meaningful name (meaningful = something more meaningful than tmp
;)
PS: Note that const
prevents moving from tmp
, which might be a downside on performance. However, in general when worried about performance you should first write readable code and only then measure for performance.
auto tmp = val * val;
Use this when you want to cast tmp
to an rvalue later on (std::move(tmp)
).
const auto tmp = val * val;
Use this when you don't need to std::move(tmp)
afterwards. Clear, not surprising, honors Scott Meyers Item "Use const
whenever possible".
const auto & tmp = val * val;
Don't do this. It does lifetime extension, but you don't gain anything from that compared to 2., because the object has to live somewhere anyhow. When val*val
returns an object by value, you won't copy it with 2. due to (N)RVO (assuming that operator *
for val
objects behaves sanely), so const auto& tmp
and const auto tmp
will both refer to an object within the stack of your scope.
auto && tmp = val * val;
const auto && tmp = val * val;
Doesn't yield any performance benefits, but complicates reading of the code. Don't do it.