Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ auto cannot deduce type with default template parameter

Tags:

c++

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?

like image 305
Anthony Poole Avatar asked Sep 05 '25 03:09

Anthony Poole


1 Answers

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<>;
like image 178
Sam Varshavchik Avatar answered Sep 08 '25 01:09

Sam Varshavchik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!