Can C++ deduce argument type from default value? [duplicate]

The type of a template parameter in a function can’t be deduced from a default argument. As shown in the example on cppreference.com:

Type template parameter cannot be deduced from the type of a function
default argument:

template<typename T> void f(T = 5, T = 7); 

void g()
{
    f(1);     // OK: calls f<int>(1, 7)
    f();      // error: cannot deduce T
    f<int>(); // OK: calls f<int>(5, 7)
}

However, you can specify a default argument for the template parameter:

template<typename A, typename B = int>
void func(int i1, int i2, A a, B b = 123){
    ...
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)