Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is pass by value and pass by rvalue overload c++ function call ambiguous?

Tags:

If I have,

void foo(Bar c);
void foo(Bar&& c);

foo(Bar()); 

why is the call to 'foo' is ambiguous? Isn't Bar() in the foo argument clearly an rValue?

like image 453
user695652 Avatar asked Apr 18 '16 14:04

user695652


People also ask

How to fix call of overloaded is ambiguous?

There are two ways to resolve this ambiguity: Typecast char to float. Remove either one of the ambiguity generating functions float or double and add overloaded function with an int type parameter.

What is ambiguity in function overloading?

When the compiler is unable to decide which function it should invoke first among the overloaded functions, this situation is known as function overloading ambiguity. The compiler does not run the program if it shows ambiguity error.

Can you pass an rvalue by reference?

You can pass an object to a function that takes an rvalue reference unless the object is marked as const . The following example shows the function f , which is overloaded to take an lvalue reference and an rvalue reference. The main function calls f with both lvalues and an rvalue.

Why do we need Rvalue reference?

Rvalue references allow programmers to avoid logically unnecessary copying and to provide perfect forwarding functions. They are primarily meant to aid in the design of higer performance and more robust libraries.


1 Answers

Binding to a reference is an "exact match", as is binding to a non-reference, so both overloads are equally good.

In Standardese, this is 13.3.3.1.4 ("Reference binding", [over.ics.ref]):

When a parameter of reference type binds directly (8.5.3) to an argument expression, the implicit conversion sequence is the identity conversion [...]

like image 173
Kerrek SB Avatar answered Oct 02 '22 08:10

Kerrek SB