Anthony Poole
Anthony Poole

Reputation: 370

C++ auto cannot deduce type with default template parameter

I notice that if I write:

template<typename T = int> void Test(T t) {};

then the following code does not compile:

auto x = Test;

However, this code does, and deduces x to be void (*x)(int t):

auto x = Test<int>;

I'm just wondering why it does not just assume the type parameter is int in the first case and treat it the same as the second one?

Upvotes: 4

Views: 417

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118300

If you have a function with a default parameter, for example:

int f(int n=0);

Calling this function with this parameter defaulted is done by doing this:

int g=f();

and not this:

int g=f;

Same principle applies to template instantiations:

auto x = Test<>;

Upvotes: 4

Related Questions