Does C++ standard guarantee the following?:
template<typename T>
void function(T (&)[1]);
template<typename T>
void function(T*);
int a[1];
function(a); // first function gets called, not second version
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, this is guaranteed, but the reason is different than what GMan says. The “array of length 1” overload will be selected because it is more specialized than the second in template functions partial order. Basically, it means that an argument in the form
T(&)[1]will always match the second template argument in the formT*, so the first overload will always be selected when conversion sequences don’t decide.From 13.3.3:
Normal functions are only affected by the first item; when any template functions are in the set of candidate functions, the second or third item can decide. The reason we want it like that is we want to be able to write seemingly ambiguous templated overloads. Eg.
would otherwise be ambiguous for
int*. In C++0x, you can even write declarations like:and the second will be selected whenever there is at least one argument.