implementing std::variant and std::visit in C++17 when handling multiple types causing static assertion failures
I'm getting frustrated with I'm stuck on something that should probably be simple. I'm trying to leverage `std::variant` along with `std::visit` for a simple type-safe union in my C++17 application. I have a variant that can hold either an `int`, `double`, or a `std::string`, but I'm running into static assertion failures when I attempt to use `std::visit` with a lambda that should handle all these types. Here's the relevant code snippet: ```cpp #include <iostream> #include <variant> #include <string> std::variant<int, double, std::string> myVariant; void handleVariant(const std::variant<int, double, std::string>& v) { std::visit([](auto&& arg) { std::cout << arg << '\n'; }, v); } int main() { myVariant = "Hello, world!"; handleVariant(myVariant); return 0; } ``` The behavior I'm working with is: ``` behavior: static assertion failed due to requirement 'is_invocable_v<decltype(lambda), int> || is_invocable_v<decltype(lambda), double> || is_invocable_v<decltype(lambda), std::string>' ``` I've checked the number of types in my variant and they definitely match the lambda's parameter types, yet I'm still getting this static assertion failure. I've tried ensuring my lambda captures by value and also tried explicitly specifying the types in the lambda argument, but nothing seems to work. Is there something I'm missing in the setup of the variant or in the use of std::visit? I want to make sure I adhere to best practices while using these facilities. Any insights would be greatly appreciated! For context: I'm using C++ on Windows. What's the best practice here? This is happening in both development and production on Debian.