I have a code structure where I grab an overloaded function pointer, check which overload to execute, static_cast
the pointer to its correct overload and runs it.
The problem is, the caller function, which is responsible for taking a pointer to any overload to this function, can't know the list of existing overloads. That being, code such as this :
auto ptr = &foobar;
invoke_correct_overload<decltype(ptr)>(ptr);
Cannot work, because the first line is ambiguous. Is there a way to specify that the overload doesn't matter in the first line, or that it should be the first overload the compiler finds ?
Context
I have a variable number of std::variant
, and need to feed it to different functions depending on the value they hold. I have figured out a way with a constexpr array to find which overload to execute, and am writing a function that takes a pointer to a function, casts all the variants according to an index in the array, and executing it.
Current attempt
#define GENERAL_INTERFACE(SPE_FUNC, OPERANDS) do {\
int valid_comb_index = evaluate_valid_types(valid_args_##SPE_FUNC, OPERANDS)); \
constexpr [[maybe_unused]] auto foo = SPE_FUNC; \
invoke_right_overload<valid_args_##SPE_FUNC.size(), \
valid_args_##SPE_FUNC[0].size() - 1, \
valid_args_##SPE_FUNC, decltype(foo)> \
(valid_comb_index, OPERANDS,foo); \
template<std::size_t N, std::size_t P,
const std::array<std::array<int, 1>, N>& valid_args, typename T>
void invoke_right_overload(int valid_comb_index,
std::vector<Operand> operands, T& funcptr) {
if(valid_comb_index == P - 1) {
auto to_invoke = static_cast<void(*)(decltype(std::get<valid_args[P][0]>(Operand())))>(funcptr);
std::invoke(to_invoke, std::get<valid_args[P][0]>(operands[0]));
}
if constexpr (P - 1 == 0) return;
invoke_right_overload<N, P - 1, valid_args, T>(valid_comb_index,
operands, funcptr, apply_to);
}
The problem is with the function that calls invoke_right_overload. There are multiple "overloads" of invoke_right_overload for 0, 1, 2 or 3 operands, so I can't get the overload using the first valid combination, for there may be none.
I know this question isn't really well-formed, but cannot find any specific point to make better. I will edit the question as much as you like for more information.