c++templatestypeid

Why doesn't default template type refer to specified type in instantiation?


#include <typeinfo>
#include <iostream>
using namespace std;

template<typename T, typename U = T> void f1(T a, U b)
{
  cout<<typeid(a).name()<<endl;
  cout<<typeid(b).name()<<endl;
}

int main()
{
  f1<float>(1,2);
}

output:

f

i ----> why is it int not default float as per template instantiation?


Solution

  • Default template arguments are last resort, and used only when the argument wasn't specified or deduced.

    It's specified in [temp.deduct]/5:

    The resulting substituted and adjusted function type is used as the type of the function template for template argument deduction. If a template argument has not been deduced and its corresponding template parameter has a default argument, the template argument is determined by substituting the template arguments determined for preceding template parameters into the default argument. If the substitution results in an invalid type, as described above, type deduction fails. [ Example:

    template <class T, class U = double>
    void f(T t = 0, U u = 0);
    
    void g() {
      f(1, 'c');        // f<int,char>(1,'c')
      f(1);             // f<int,double>(1,0)
      f();              // error: T cannot be deduced
      f<int>();         // f<int,double>(0,0)
      f<int,char>();    // f<int,char>(0,0)
    }
    

    — end example ]