Throw exception on missing function overload with std::variant instead of compile time error
- 阅读更多 关于 Throw exception on missing function overload with std::variant instead of compile time error
问题 This is a follow up to this question Consider the following code #include <variant> int add_(int a, int b){ return a+b; } float add_(float a, float b){ return a+b; } float add_(int a, float b){ return a+b; } float add_(float a, int b){ return a+b; } using Number = std::variant<int, float>; Number add(Number const& lhs, Number const& rhs ){ return std::visit( []( auto& lhs_, auto& rhs_ )->Number { return {add_( lhs_, rhs_ )}; }, lhs, rhs ); } int main(){ Number a = 1.f; Number b = 2.f; Number