OK so I was cooking up an answer here which has more detail (and a better alternative.) But I realized I'd made a couple template functions which had a lot of redudancy. Given:
template<typename T>
struct Parent {};
struct Child : Parent<int> {};
I wrote the following template functions for getting the appropriate Parent
pointer:
namespace details {
template<typename T>
Parent<T>* make_parent(Parent<T>* param) { return param; }
}
template<typename T>
auto make_parent(T& param) -> decltype(details::make_parent(¶m)) { return details::make_parent(¶m); }
There seems to be a lot of repetition in there. But I can't figure out how to make it cleaner. Can I combine this into a single function without it looking like a nightmare?
EDIT:
My intention is that I can do:
Child foo;
auto bar = make_parent(foo);
(As opposed to the easier version in the other answer, where I pass a pointer.)