Search code examples
c++templatesfunction-templates

Which of two overloaded templates will be called?


I am still trying to figure out templates. I have read about the specialization rules and don't understand what is happening here.

I have defined the following in templates.h:

#include <iostream>

template <typename foo>
void f(foo p)
{
  std::cout << "one" << std::endl;
}

template <typename bar>
void f(int p)
{
  std::cout << "two" << std::endl;
}

Now if I include this and call it in my main like this

  f(1);
  f("x");

I get

one
one

Now the questions is, why is the first more specific than the second for ints? I feel like it should at least be ambiguous and not work at all.


Solution

  • The second overload one has no template dependency on function arguments, so you would have to call it like this:

    f<std::string>(1);
    f<double>(42);
    f<SomeType>(1);
    

    Whether it makes sense to have the second version is a different matter. You could imagine having a template parameter that does have some effect on the internal logic of the function:

    template <typename SomeType>
    int foo(int seed) {
      // instantiate a SomeType and use it to calculate return value
    };
    
    int i = foo<Type1>(42);
    int j = foo<Type2>(42);
    

    On the other hand, your SomeType could be a function parameter:

    template <typename SomeType>
    int foo(int seed, const SomeType& s) {
      // use s to calculate return value
    };
    
    Type1 t1;
    Type2 t2;
    int i = foo(42, t1);
    int j = foo(42, t2);