I have a class and a class template
class promise;
template <class... Ts>
class typed_promise;
I intend to design a cast utility to cast between them
template <class... Ts>
typed_promise<Ts...> promise_cast(promise x);
template <class... Ts>
promise promise_cast(typed_promise<Ts...> x);
So, you can do the following cast
promise pms;
typed_promise<int, int> typed_pms;
auto typed_pms2 = promise_cast<int, int>(pms);
auto pms2 = promise_cast(typed_pms);
I also want to support usage like
auto typed_pms2 = promise_cast<typed_promise<int, int>>(pms);
which is equivalent to
auto typed_pms2 = promise_cast<int, int>(pms);
Since C++ does not allow partial function template specialization, is it possible to achieve what I want? If possible, how should I do it?
Dispatch to a class template that you can partially specialize.
template<class... Ts>
struct promise_cast_impl {
static typed_promise<Ts...> do_cast(promise) {
// do stuff
}
};
template<class... Ts>
struct promise_cast_impl<typed_promise<Ts...>> {
static typed_promise<Ts...> do_cast(promise p){
return promise_cast_impl<Ts...>::do_cast(std::move(p));
}
};
template<class... Ts>
auto promise_cast(promise x)
-> decltype(promise_cast_impl<Ts...>::do_cast(std::move(x))){
return promise_cast_impl<Ts...>::do_cast(std::move(x));
}
If you don't use Ts...
anywhere else, you can simplify this slightly by writing a metafunction that just computes the return type from Ts...
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments