Function overload for string literals lvalue and rvalue reference
Thank you @songyuanyao for your answer, I understand now why test(char*&&)
is chosen in the two last cases. I was able to remove ambiguity with template specialization on first overload thanks to @Darklighter answer too.
So I solved my problem such as below :
#include <iostream>
template <unsigned long int N>
void test(const char (&)[N]){
std::cout << __PRETTY_FUNCTION__ << " //non-empty literal" << std::endl;
}
template <>
void test(const char (&)[1]){
std::cout << __PRETTY_FUNCTION__ << " //empty literal" << std::endl;
}
void test(char*&&){
std::cout << __PRETTY_FUNCTION__ << " //string variable" << std::endl;
}
int main(){
char str1[] = "";
char str2[] = "test";
test("");
test("test");
test(str1);
test(str2);
}
Output :
clang++ test.cpp -o test.out && ./test.out
void test(const char (&)[1]) //empty literal
void test(const char (&)[N]) [N = 5] //non-empty literal
void test(char *&&) //string variable
void test(char *&&) //string variable
g++ test.cpp -o test.exe && test.exe
void test(const char (&)[N]) [with long unsigned int N = 1] //empty literal
void test(const char (&)[N]) [with long unsigned int N = 5] //non-empty literal
void test(char*&&) //string variable
void test(char*&&) //string variable
- Which compiler is correct ?
GCC is correct.
- With clang, why str1 and str2 choose the rvalue overload while they are lvalues ?
Clang is wrong on test(str1);
, it should be ambiguous. For test(str2);
, str2
could convert to pointer implicitly, i.e. the array-to-pointer decay. The converted char*
is an rvalue. For the same reason as #3, the implicit conversion sequences have the same ranking, then non-template function is prefered; test(char*&&)
is selected.
- With gcc, why call with str1 is ambiguous ?
For test(const char (&)[1])
to be called, qualification conversion from char[1]
to const char[1]
is required; for test(char*&&)
to be called, array-to-pointer conversion is required. Both are qualified as exact match and have the same ranking.
- Is there a standard rule for this situation ?
See the ranking of implicit conversion sequences in overload resolution, and implicit conversions.
- How to fix the two last calls ?
It depends on your intent.
String literals are not rvalues. (→)
- How to fix the two last calls?
You can disambiguate everything with template specializations:
#include <iostream>
template<typename C, std::size_t N>
void test(const C (&)[N]) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
template<typename C>
void test(const C (&)[1]) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
template<typename C, std::size_t N>
void test(const C (&&)[N]) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
template<typename C>
void test(const C (&&)[1]) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
template<typename C, std::size_t N>
void test(C (&)[N]) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
template<typename C>
void test(C (&)[1]) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
template<typename C, std::size_t N>
void test(C (&&)[N]) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
template<typename C>
void test(C (&&)[1]) { std::cout << __PRETTY_FUNCTION__ << std::endl; }
int main(){
char str1[] = "";
char str2[] = "test";
test("");
test("test");
test(str1);
test(str2);
test(std::move(str1));
test(std::move(str2));
const char str3[] = "";
const char str4[] = "test";
test(std::move(str3));
test(std::move(str4));
}
gives
void test(const C (&)[1]) [with C = char]
void test(const C (&)[N]) [with C = char; long unsigned int N = 5]
void test(C (&)[1]) [with C = char]
void test(C (&)[N]) [with C = char; long unsigned int N = 5]
void test(C (&&)[1]) [with C = char]
void test(C (&&)[N]) [with C = char; long unsigned int N = 5]
void test(const C (&&)[1]) [with C = char]
void test(const C (&&)[N]) [with C = char; long unsigned int N = 5]