#include <iostream>
using namespace std;
void func(int (&ref)[6]) { cout << "#1" << endl; }
void func(int * &&ref) { cout << "#2" << endl; }
int main()
{
int arr[6];
func(arr); // g++(5.4): ambiguous, clang++(3.8): #2, vc++(19.11): #1
return 0;
}
Both functions are exact matches. Below is a quote from the standard:
Standard conversion sequence S1 is a better conversion sequence than standard conversion sequence S2 if
...
S1 and S2 are reference bindings (8.5.3) and neither refers to an implicit object parameter of a non-static member function declared without a ref-qualifier, and S1 binds an rvalue reference to an rvalue and S2 binds an lvalue reference.
Doesn't it imply that the second is better?
Updated:
There is a related question. And the following code is a simplified version of it.
#include <iostream>
using namespace std;
void func(int *&) { cout << "#1" << endl; }
void func(int *&&) { cout << "#2" << endl; }
int main()
{
int arr[6];
func(arr); // g++(5.4) and clang++(3.8): #2, vc++(19.11): ambiguous
return 0;
}
int (&ref)[6]
is an "lvalue reference", as referenced in the quote. Even though this is a reference, it can be argued that this is not a reference to an lvalue. If it were, you could assign toref
, i.e.ref=<something>
. But you can't. – Dufrene&
is called an lvalue reference". – Mightily