1

I don't understand why this program produces the output below.

void blah(const char* )       {printf("const char*\n");}
void blah(const std::string&) {printf("const string ref\n");}
template<class t>
void blah(t)                  {printf ("unknown\n");}

int main(int, char*)
{        
  blah("hi");
  char a[4];
  blah(a);
  std::string s;
  blah(s);
  getch();
}

Outputs:

const char*
unknown
const string

In VS2008. It is willing to convert the std::string to a const reference, but why won't it convert the char* to a const char* and use the overload?

4
  • 1
    it's simply choosing the closest match from the options you've given it. class t can match char* exactly, with no conversion needed, so it chooses that one in preference.
    – Dave
    Jun 7, 2013 at 22:29
  • (incidentally, the string example isn't being converted to a reference, but rather it defaults to being passed by reference and only becomes a pass-by-copy if that has no matches. Templates can't change the reference-ness (except for a special case for rvalue references), so that template can only accept classes by copy)
    – Dave
    Jun 7, 2013 at 22:32
  • What do you mean that the string "defaults to being passed by reference"? Why can't the type t be std::string&? What gets me is that neither calls are exact matches. Neither the string nor the char* is const. Jun 7, 2013 at 23:38
  • As I said, "it defaults to being passed by reference and only becomes a pass-by-copy if that has no matches". In other words, it tries string& then const string& then string and const string. The template can only be the latter 2 (because "Templates can't change the reference-ness"). The first doesn't exist, so the first good match is the second.
    – Dave
    Jun 7, 2013 at 23:44

1 Answer 1

3

The type of "hi" is const char[3], whereas the type of a is char[4].

So, the first call requires only array-to-pointer conversion (aka "decay"). The third call requires only binding an object to a reference-to-const (I don't think "converting" is the correct terminology for reference-binding, although I may be mistaken). The second call would require array decay and a pointer conversion in order to call the const char* overload.

I claim without actually checking the overload resolution text in the standard that this extra step is what makes the template a better match than the const char* overload.

Btw, if you change "unknown\n" to "%s\n", typeid(t).name() then you can see what the type t was deduced as. For your code, it is deduced as char* (because arrays can't be passed by value), but see what happens if you change the template to take a t& parameter instead of t. Then t can be deduced as char[4].

6
  • This seems incorrect. blah((char*)"hi") still produces unknown. Jun 7, 2013 at 23:35
  • @TysonJacobs of course it does. Just as blah((const char*)a) will produce const char*. An explicit cast happens before function resolution, so the function call would have to convert it back!
    – Dave
    Jun 7, 2013 at 23:41
  • @Dave but your point was that the template matched the call to blah("hi") because it had to "decay" the array to a pointer and convert the pointer type. Casting to char* eliminates one of those steps. Jun 7, 2013 at 23:45
  • @TysonJacobs one of us is confused. blah("hi") goes to const char*, whereas the cast changes that to unknown. That is exactly the behaviour I would expect from the rules me and Steve Jessop mentioned, so I don't see the problem…
    – Dave
    Jun 7, 2013 at 23:47
  • 1
    @TysonJacobs: blah((char*)a) requires a pointer conversion to call the const char* overload. So the fact that it selects the template seems entirely consistent with my claim/guess that it's the pointer conversion that makes the const char* overload a worse match than the template. The interesting thing about the array decay, is that although it's a conversion it doesn't result in a worse match. In short the reason for the asymmetry is that if you start with a non-const array or ptr then you need a pointer conversion to call the const char* overload. With a const array you don't. Jun 8, 2013 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.