Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force template class to use specified type unless specified otherwise?

I have problem with my template class. I specified default type of my template class like that:

template < class T = float >
class apple { 
public:
    T x;
    apple(T x): x(x) {}
}

However, when I create the object like that:

apple obj(2);

The type turns into int unless I do that:

apple<float> obj(2);

How would I make it stay float?

like image 995
Marcin Poloczek Avatar asked Jan 01 '23 19:01

Marcin Poloczek


1 Answers

Add this deduction guide to force all argument deductions to resolve to your default arguments:

template <class T>
apple(T) -> apple<>;
like image 107
Quentin Avatar answered Jan 13 '23 14:01

Quentin