Hi,
In my current project, I would like to use std::visit and std::variant, where the return type of std::visit should be a std::variant which template arguments should be automatically deduced by the compiler.
Here is an example:
struct Test {
constexpr auto operator()(auto&& arg) { // <--- Just using auto as return type does not work.
// I guess I somehow have to specify std::variant together with variadic templates?
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, int>) {
return float(arg);
} else {
return int(arg);
}
}
};
int main(){
using Variant = std::variant<int, float, double>;
auto a = Variant(4.5f);
auto t = std::visit(Test{}, a);
}
I want the return type of operator() in this example to be deduced as std::variant<int, float>.
I know I could explicitly write
auto operator()(auto&& arg) -> std::variant<int, float> { ...}
But I would like to let the compiler deduce the template arguments of the return type (int and float) based on what is going on within the operator() method.
Is that even possible?
