Syntactic doubt for specialized function templates

Suppose I have a function template where the type parameter is only used as the return type:

template <typename T>
T foo()
{
    return whatever;
}

Then what is the correct syntax to specialize this function template? Both of them seem to work:

template <>
std::string foo()
{
    return whatever;
}

template <>
std::string foo<std::string>()
{
    return whatever;
}

Is there a difference between the two? If not, what is the idiomatic way?

+5
source share
2 answers

The compiler will infer the correct specialized specialization based on the information provided (here, the type of the returned function).

Thus, these syntaxes have exactly the same behavior as being more explicit than the other.

+1

.

, , , , ( , , , , , ).

+1

All Articles