I have a function that returns a template class. In this function, the return statement calls a template function that creates an instance of the template class. Is it possible, in the following situation, to infer the template function's template arguments?
#include <utility>
#include <variant>
template <class T, class U>
auto make(T&& t) -> std::variant<T, U> {
return std::variant<T, U>{ std::forward<T>(t) };
}
auto foo() -> std::variant<int, char> {
return make<int, char>(42); // return make(42); instead?
}
You can have make
wrap whatever you give it in an object that has a conversion to the required specialization of std::variant
:
template<typename T>
struct make {
make(T &&x) : x(x) { }
T &x;
template<typename... Us>
operator std::variant<T, Us...>() {
return std::forward<T>(x);
}
};
template<typename T>
make(T&&) -> make<T>;