I am looking for a class that is like std::variant that holds all types instead of one of the types.
So I would like to be able to write:
std::all_variant<int, double> av{1, 3.14159};
int i = std::get<int>(av); // i should be 1.
double d = std::get<double>(av); // d should be 3.14159
This is basically a tuple but guarantees that each type appears only once so you can do std::get<Type> instead of std::get<Index>.
For context I want to be able to write the following code (so if there is a better way of doing that I would appreciate to know):
std::all_variant<int, double> av{1, 3.14159};
template <typename T> // T must be either int or double.
void MyFunc(std::all_variant<int, double> av) {
std::cout << std::get<T>(av);
}
int main() {
MyFunc<int>(av); // Should print 1.
MyFunc<double>(av); // Should print 3.14159.
MyFunc<std::string>(av); // Error! av doesn't have std::string variant.
}
I'm not sure what this would be called so I'm not sure if this is in the standard library. I guess it wouldn't be to hard to write one but I don't want to reinvent the wheel.