Need to understand and figure out how to work efficiently with these 2 definitions on lowlevel.
void func(const T& foo)
{
T bar = foo;
}
// Lets overload this
void func(T&& foo)
{
T bar = foo;
}
Here is what I know already.
T&& foo
is for rvalue which is not creating copy of your param that you are pass through function. Ex : func(5)
works nice.const T& foo
is also okay for the rvalue.T&& foo
cannot pass lvalue.So what is the fundemental reason, whats the main get of differenciates usage? I'm not expecting const T&
also doesnt create copy for passed param as well. Or is it ? So went to oblivion, need clear answer on this.
Edit : I guess need to do addition. Asked question as int
but this is mainly valid for any class type which we are going to avoid copies.
Edit : Converting question on generic typename T, to be more certain on questions intentions.
"So what is the fundemental reason, whats the main get of differenciates usage?"
If both of your overload do exactly the same thing, there is no point of having the rvalue-qualified one since a T &&
can be bound to a const T &
(and you're already aware of that).
The need to have the rvalue overload is when you need to do something different if an rvalue is passed (for example, move-assign the object into another one or anything else you could think of).
"I'm not expecting const int& also doesnt create copy for passed param as well. Or is it ?"
A reference, no matter how qualified, is still a reference. So whether you accept your argument through a const T &
or a T &&
, no copy is made.