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 c = add(a, b);
}
By adding more and more types to number and potentially having function that depend on more than 2 arguments, it quickly becomes clear, that I need to define a lot of functions, namely all possible parameter combinations that are found in the std::variant.
However, not all combinations are possible and/or needed. How can I only define the functions I need and throw an exception if I try to call a function that is not overloaded for the particular parameter combination? For example, let's say I only want to keep the add_(int, int) or add_(float, float) function.