Using variant variable as input to other variant function

165 Views Asked by At

I am using two different variants where one can hold more types than the other.

typedef std::variant<boost::blank, double> EmptyDouble;
typedef std::variant<boost::blank, double, int> EmptyDoubleOrInt;

Now i want to use a function having the EmptyDoubleOrInt as parameter. This function works fine for an integer, double or blank, but not for a EmptyDouble type:

SomeFunction(EmptyDoubleOrInt input);

How can i convert the EmptyDouble to either a blank or double without having to know what type it has. So it will be a correct input for the function?

SomeFunction(DoSomething(emptyDouble))
1

There are 1 best solutions below

1
Holt On

You can use std::visit to construct EmptyDoubleOrInt from emptyDouble:

SomeFunction(std::visit([](const auto& a) -> EmptyDoubleOrInt {
    return a;
}, emptyDouble));