function

calling functions bound to Lua

Note

This abstraction assumes the function runs safely. If you expect your code to have errors (e.g., you don’t always have explicit control over it or are trying to debug errors), please use sol::protected_function explicitly.

class unsafe_function : public reference;
typedef unsafe_function function;

Function is a correct-assuming version of protected_function, omitting the need for typechecks and error handling (thus marginally increasing speed in some cases). It is the default function type of Sol. Grab a function directly off the stack using the constructor:

constructor: function
function(lua_State* L, int index = -1);

When called without the return types being specified by either a sol::types<...> list or a call<Ret...>( ... ) template type list, it generates a function_result class that gets implicitly converted to the requested return type. For example:

func_barks.lua
1
2
3
4
5
bark_power = 11;

function woof ( bark_energy )
        return (bark_energy * (bark_power / 4))
end

The following C++ code will call this function from this file and retrieve the return value:

1
2
3
4
5
6
sol::state lua;

lua.script_file( "func_barks.lua" );

sol::function woof = lua["woof"];
double numwoof = woof(20);

The call woof(20) generates a function_result, which is then implicitly converted to an double after being called. The intermediate temporary function_result is then destructed, popping the Lua function call results off the Lua stack.

You can also return multiple values by using std::tuple, or if you need to bind them to pre-existing variables use sol::tie:

1
2
3
4
5
6
7
8
9
sol::state lua;

lua.script( "function f () return 10, 11, 12 end" );

sol::function f = lua["f"];
std::tuple<int, int, int> abc = f(); // 10, 11, 12 from Lua
// or
int a, b, c;
sol::tie(a, b, c) = f(); // a = 10, b = 11, c = 12 from Lua

This makes it much easier to work with multiple return values. Using std::tie from the C++ standard will result in dangling references or bad behavior because of the very poor way in which C++ tuples/std::tie were specified and implemented: please use sol::tie( ... ) instead to satisfy any multi-return needs.

Warning

Do NOT save the return type of a function_result with auto, as in auto numwoof = woof(20);, and do NOT store it anywhere. Unlike its counterpart protected_function_result, function_result is NOT safe to store as it assumes that its return types are still at the top of the stack and when its destructor is called will pop the number of results the function was supposed to return off the top of the stack. If you mess with the Lua stack between saving function_result and it being destructed, you will be subject to an incredible number of surprising and hard-to-track bugs. Don’t do it.

function: call operator / function call
template<typename... Args>
function_result operator()( Args&&... args );

template<typename... Ret, typename... Args>
decltype(auto) call( Args&&... args );

template<typename... Ret, typename... Args>
decltype(auto) operator()( types<Ret...>, Args&&... args );

Calls the function. The second operator() lets you specify the templated return types using the my_func(sol::types<int, std::string>, ...) syntax. Function assumes there are no runtime errors, and thusly will call the atpanic function if an error does occur.

To know more about how function arguments are handled, see this note