resolve¶
utility to pick overloaded C++ function calls
template <typename... Args, typename F>
constexpr auto resolve( F f );
resolve is a function that is meant to help users pick a single function out of a group of overloaded functions in C++. It works for both member and free functions You can use it to pick overloads by specifying the signature as the first template argument. Given a collection of overloaded functions:
1int overloaded(int x);
2int overloaded(int x, int y);
3int overloaded(int x, int y, int z);
4
5struct thing {
6 int overloaded() const;
7 int overloaded(int x);
8 int overloaded(int x, int y);
9 int overloaded(int x, int y, int z);
10};
You can disambiguate them using resolve:
1auto one_argument_func = resolve<int(int)>( overloaded );
2auto two_argument_func = resolve<int(int, int)>( overloaded );
3auto three_argument_func = resolve<int(int, int, int)>( overloaded );
4auto member_three_argument_func = resolve<int(int, int, int)>( &thing::overloaded );
5auto member_zero_argument_const_func = resolve<int() const>( &thing::overloaded );
It is important to note that const is placed at the end for when you desire const overloads. You will get compiler errors if you are not specific and do not properly disambiguate for const member functions. This resolution also becomes useful when setting functions on a table or state_view:
1sol::state lua;
2
3lua.set_function("a", resolve<int(int)>( overloaded ) );
4lua.set_function("b", resolve<int(int, int)>( overloaded ));
5lua.set_function("c", resolve<int(int, int, int)>( overloaded ));
It can also be used with sol::c_call:
1sol::state lua;
2
3auto f = sol::c_call<
4 decltype(sol::resolve<int(int, int)>(&overloaded)),
5 sol::resolve<int(int, int)>(&overloaded)
6>;
7lua.set_function("f", f);
8
9lua.script("f(1, 2)");
Note
You cannot use sol::resolve<...>(...) when one function is templated and it has a non-templated overload: it will always fail in this case. To resolve this, please use a manual static_cast<R(Args...)>( &func ) or static_cast<R (T::*)(Args...)>( &T::overloaded_member_func ) (with the right const-ness and volatile-ness and r-value/l-value qualifiers if necessary).