Let's say I have a function that wants to deduce template template parameters to handle types that encapsulate other types, like std::optional
:
template <template <typename> typename M, typename T>
void Foo(const M<T>& input);
// Deduces M = std::optional, T = int
Foo(std::make_optional(0));
This is great, but it accepts the input only as an lvalue reference. What if I want to accept any sort of reference, and pass it through to Bar
with perfect forwarding? Without the deduction of M
this is easy:
template <Input>
void Foo(Input&& input) {
Bar(std::forward<Input>(input));
}
But now I've lost the deduction of M
. Is there any way to deduce M
and get something as convenient as a forwarding reference? I would like to avoid defining the function four to eight times if Bar
needs to know the template M
:
template <template <typename> typename M, typename T>
void Foo(M<T>& input);
Bar<M>(input);
}
template <template <typename> typename M, typename T>
void Foo(const M<T>& input);
Bar<M>(input);
}
template <template <typename> typename M, typename T>
void Foo(M<T>&& input);
Bar<M>(std::move(input));
}
template <template <typename> typename M, typename T>
void Foo(const M<T>&& input);
Bar<M>(std::move(input));
}