template function call with empty angle brackets <>

7.6k views Asked by At

I am confused with the below template behavior, where it compiles fine with the empty angle brackets (template without parameters) since syntactically, template<> is reserved to mark an explicit template specialization.

template <typename T> void add(T a, T b) { }
int main() {
    add<>(10, 3); // compiles fine since both parameters are of same data type
    add<>(10, 3.2); // Error: no matching function for call to add(int, double)
}

In the above case is the template parameter really optional?

3

There are 3 answers

0
Paul Evans On

In the first case, yes because the can be inferred through the standard's rules. In the second, no because they can't - you'd have to write something like:

add<float>(10, 3.2);
0
TemplateRex On

You have a single template parameter and two function parameters of different types. Template argument deduction needs to match for both arguments, but if you supply an int and a double, it doesn't work. The reason is that deduced argument have to an exact match and type conversions are not considered.

The syntax

add<double>(10, 3.2); 

would explicitly force T to be equal to double. In that case, the int constant 10 is converted to double.

You could also add another overload

template <typename T, typename U> void add(T a, U b) { }

and possibly constrain that using SFINAE by requiring that is_convertible<T, U>

0
Mike Seymour On

template<> is reserved to mark an explicit template specialization.

It means various things, depending on context. Here it means "use the default or deduced argument", just as if you simply said add.

In the first case, both function arguments have the same type, so the template argument can be deduced as int.

In the second case, they have different types, so the template argument can't be deduced. You'd have to specify what you want, e.g. add<double>, convert one function argument to match the other, or modify the template to parametrise each type separately.

In the above case is the template parameter really optional?

Yes, if it can be deduced from the argument types.