Let's say I've got a templated class, and a function that accepts it in a shared pointer to const, using its template parameter as part of its signature:
template <class T>
class SomeClass {};
// Doesn't need to modify the SomeClass object.
template <class T>
T DoSomething(std::shared_ptr<const SomeClass<T>>);
In this case I can call DoSomething
using a shared pointer to non-cost SomeClass
by explicitly specifying the template parameter:
DoSomething<int>(std::make_shared<SomeClass<int>>());
But this doesn't work without being explicit, because type deduction fails.
How can I make the function callable with type deduction in this case? Obviously I could write another overload that accepted a shared pointer to non-const, but it's a drag to need to define every function of this type twice.
Ideally this would fail for inputs that aren't correct (shared pointers to other things, or non-shared pointers) at overload resolution time.
One possibility is to use a helper to check for the right type while letting
DoSomething
accept anyT
(and fail to compile for the wrongT
):You'll might need to do something extra to get the return type correctly deduced.