summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/sol2/docs/source/api/resolve.rst
blob: 193e2fae80f95d7d608e962577229050ba1755da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
resolve
=======
utility to pick overloaded C++ function calls
---------------------------------------------

.. code-block:: cpp
	:caption: function: resolve C++ overload

	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:

.. code-block:: cpp
	:linenos:

	int overloaded(int x);
	int overloaded(int x, int y);
	int overloaded(int x, int y, int z);

	struct thing {
		int overloaded(int x);
		int overloaded(int x, int y);
		int overloaded(int x, int y, int z);
	};

You can disambiguate them using ``resolve``:

..  code-block:: cpp
	:linenos:

	auto one_argument_func = resolve<int(int)>( overloaded );
	auto two_argument_func = resolve<int(int, int)>( overloaded );
	auto three_argument_func = resolve<int(int, int, int)>( overloaded );
	auto member_three_argument_func = resolve<int(int, int, int)>( &thing::overloaded );

This resolution becomes useful when setting functions on a :doc:`table<table>` or :doc:`state_view<state>`:

..  code-block:: cpp
	:linenos:

	sol::state lua;

	lua.set_function("a", resolve<int(int)>( overloaded ) );
	lua.set_function("b", resolve<int(int, int)>( overloaded ));
	lua.set_function("c", resolve<int(int, int, int)>( overloaded ));


It can also be used with :doc:`sol::c_call<c_call>`:

.. code-block:: cpp
	:linenos:

	sol::state lua;
	
	auto f = sol::c_call<
		decltype(sol::resolve<int(int, int)>(&overloaded)), 
		sol::resolve<int(int, int)>(&overloaded)
	>;
	lua.set_function("f", f);
	
	lua.script("f(1, 2)");