metatable_key

a key for setting and getting an object’s metatable

struct metatable_key_t {};
const metatable_key_t metatable_key;

You can use this in conjunction with sol::table to set/get a metatable. Lua metatables are powerful ways to override default behavior of objects for various kinds of operators, among other things. Here is an entirely complete example, showing getting and working with a usertype’s metatable defined by sol:

messing with metatables
 1#define SOL_ALL_SAFETIES_ON 1
 2#include <sol/sol.hpp>
 3
 4
 5int main(int, char*[]) {
 6
 7	struct bark {
 8		int operator()(int x) {
 9			return x;
10		}
11	};
12
13	sol::state lua;
14	lua.open_libraries(sol::lib::base);
15
16	lua.new_usertype<bark>("bark",
17	     sol::meta_function::call_function,
18	     &bark::operator());
19
20	bark b;
21	lua.set("b", &b);
22
23	sol::table b_as_table = lua["b"];
24	sol::table b_metatable = b_as_table[sol::metatable_key];
25	sol::function b_call = b_metatable["__call"];
26	sol::function b_as_function = lua["b"];
27
28	int result1 = b_as_function(1);
29	// pass 'self' directly to argument
30	int result2 = b_call(b, 1);
31	SOL_ASSERT(result1 == result2);
32	SOL_ASSERT(result1 == 1);
33	SOL_ASSERT(result2 == 1);
34}