2
votes

From C++ Primer, I know that for template's parameter arguments, only two kinds of conversions are performed: one is const conversion, another one is array/function to pointer conversion.

However, when it comes to explicit argument, it seems that everything changes. Assume we have a template function:

template <typename T>
int compare(const T &a, const T &b)
{
    // do comparison
}

If no explicit argument involved, function call like this is illegal:

compare("foo", "foobar");

The weird thing happens (actually, it might not be weird but I do not understand) when we explicitly do:

compare<std::string>("foo", "foobar");

It seems that in the second call, "foo" and "foobar" are converted to std::string, which is controversial.

Is there any special rules for template explicit arguments? Thanks.

1
Why does this seem controversial to you? By providing the explicit template parameter you are indicating you would like to cast the corresponding T arguments to a const std::string&, which it does via a std::string constructor overload using your character string literals. What behavior were you expecting? If the T has no casting constructor (or has an explicit one) then this will not happen. - Preet Kukreti
Does it mean that when we explicit bind the T with std::string, the actual function code is generated? - Ke Li

1 Answers

6
votes

In the first case the compiler tries to deduce the type T from the given parameters. From the first parameter the compiler deduces the type const char (&)[4] (aka. reference to an array of 4 characters), from the second it gets const char (&)[7]. The two types don't match and the compiler can't figure out what T should be.

In the second example you explicitly specify that the T template parameter should be std::string. So T will be std::string. The compiler accepts the type you give and checks if the given function parameters match that type. In this case the parameters fit, because "foo" and "foobar" can be implicitly converted to std::string. (The const char[] degrade to const char*, and then there is a constructor that can construct a std::string from a const char*)