Implicit template type deduction with two arguments of different types

Viewed 1630

Assume the following situation:

Type A and type B, B can be implicitly converted to A but the opposite is untrue.

I have a function

template<class T>
void do_stuff(T a, T b);

I want to call said function as such:

do_stuff(A{}, B{});

The problem here is that the compiler can't deduce the type and instead says:

template argument deduction/substitution failed

I can call my function like this:

do_stuff<A>(A{}, B{});

But this is more annoying for the user.

Alternatively I can do something like this:

template<class T, class M>
void do_stuff(T a, M b);

But then b goes on its merry way to be of type B (with the prior invocation).

Ideally I would like something like:

template<class T, class M = T>
void do_stuff(T a, M b);

Or:

template<class T@INSERT MAGIC SO THAT T IS DEDUCED AS BEING THE TYPE OF ARGUMENT NR 1@>
void do_stuff(T a, T b);

Is such a thing possible ?

4 Answers
Related