C++17 std::optional and std::variant interaction causing unexpected initialization behavior
I'm working on a project and hit a roadblock... I'm currently working on a C++17 project where I need to use `std::optional` in conjunction with `std::variant` to represent a value that can either be an integer or nothing. However, I'm encountering a situation where my `std::optional<std::variant<int, std::string>>` seems to behave unexpectedly. When I try to initialize it with a `std::variant` value, I get strange results. Here's a simplified version of my code: ```cpp #include <iostream> #include <variant> #include <optional> #include <string> int main() { std::optional<std::variant<int, std::string>> opt; opt = std::variant<int, std::string>(42); if (opt.has_value()) { std::visit([](auto&& arg) { std::cout << "Value: " << arg << '\n'; }, *opt); } else { std::cout << "No value present\n"; } return 0; } ``` When I run this code, I expect it to print `Value: 42`, but instead, I get `No value present`. I've also tried initializing `opt` directly with an `std::optional` like so: ```cpp std::optional<std::variant<int, std::string>> opt = std::variant<int, std::string>(42); ``` This still results in the same output. I've verified that `std::variant<int, std::string>` is properly constructed, but the `std::optional` seems to not recognize it as a valid value. I checked my compiler settings and I'm using GCC 9.3.0 with the `-std=c++17` flag. Am I misunderstanding how to use `std::optional` with `std::variant`, or is there an underlying issue with my initialization? Any insights would be greatly appreciated! This is part of a larger CLI tool I'm building. How would you solve this?