Higher-kinded Types with C++
Template-template parameters?
template <template <typename> class m>
struct Monad {
template <typename a>
static m<a> mreturn(const a&);
template <typename a, typename b>
static m<b> mbind(const m<a>&, m<b>(*)(const a&));
};
template <typename a>
struct Maybe {
bool isNothing;
a value;
};
template <>
struct Monad<Maybe> {
template <typename a>
static Maybe<a> mreturn(const a& v) {
Maybe<a> x;
x.isNothing = false;
x.value = v;
return x;
}
template <typename a, typename b>
static Maybe<b> mbind(const Maybe<a>& action, Maybe<b>(*function)(const a&)) {
if (action.isNothing)
return action;
else
return function(action.value);
}
};
Isn't usually a normal template already a higher-kinded type? For example std::vector
takes a type parameter to create an actual type like std::vector<int>
, so it has kind * -> *
.