# sol2 3.5.0 documentation

## proxy, (protectedunsafe)_function_result - proxy_base derivatives

### Search the Documentation

# proxy, (protectedunsafe)_function_result - proxy_base derivatives

``table[x]`` and ``function(…)`` conversion struct

    template <typename Recurring>
    struct proxy_base;

    template <typename Table, typename Key>
    struct proxy : proxy_base<...>;

    struct stack_proxy: proxy_base<...>;

    struct unsafe_function_result : proxy_base<...>;

    struct protected_function_result: proxy_base<...>;

These classes provide implicit assignment operator `operator=` (for `set`) and an implicit conversion operator `operator T` (for `get`) to support items retrieved from the underlying Lua implementation, specifically sol::table and the results of function calls on sol::function and sol::protected_function.

## proxy

`proxy` is returned by lookups into sol::table and table-like entities. Because it is templated on key and table type, it would be hard to spell: you can capture it using the word `auto` if you feel like you need to carry it around for some reason before using it. `proxy` evaluates its arguments lazily, when you finally call `get` or `set` on it. Here are some examples given the following lua script:

    1		woof = {
    2			[2] = "arf!"
    3		}
    4	}
    5	)";

After loading that file in or putting it in a string and reading the string directly in lua (see state), you can start kicking around with it in C++ like so:

     1#define SOL_ALL_SAFETIES_ON 1
     2#include <sol/sol.hpp>
     3
     4
     5int main() {
     6
     7	lua.open_libraries(sol::lib::base);
     8	lua.script(code);
     9
    10	// produces table_proxy, implicitly converts to std::string,
    11	// quietly destroys table_proxy
    12	std::string arf_string = lua["bark"]["woof"][2];
    13
    14	// lazy-evaluation of tables
    15	auto x = lua["bark"];
    16	auto y = x["woof"];
    17	auto z = y[2];
    18
    19	// retrivies value inside of lua table above
    20	std::string value = z;
    21	SOL_ASSERT(value == "arf!");
    22
    23	// Can change the value later...
    24	z = 20;
    25
    26	// Yay, lazy-evaluation!
    27	int changed_value = z; // now it's 20!
    28	SOL_ASSERT(changed_value == 20);
    29	lua.script("assert(bark.woof[2] == 20)");

We don’t recommend using `proxy` lazy evaluation the above to be used across classes or between function: it’s more of something you can do to save a reference to a value you like, call a script or run a lua function, and then get it afterwards. You can also set functions (and function objects) this way, and retrieve them as well:

     1	lua["a_new_value"] = 24;
     2	lua["chase_tail"] = [](int chasing) {
     3		int r = 2;
     4		for (int i = 0; i < chasing; ++i) {
     5			r *= r;
     6		}
     7		return r;
     8	};
     9
    10	lua.script("assert(a_new_value == 24)");
    11	lua.script("assert(chase_tail(2) == 16)");
    12
    13	return 0;
    14}

## members

functions: [overloaded] implicit conversion get

    requires( sol::is_primitive_type<T>::value == true )
    template <typename T>
    operator T() const;

    requires( sol::is_primitive_type<T>::value == false )
    template <typename T>
    operator T&() const;

Gets the value associated with the keys the proxy was generated and convers it to the type `T`. Note that this function will always return `T&`, a non-const reference, to types which are not based on sol::reference and not a primitive lua type

function: get a value

    template <typename T>
    decltype(auto) get( ) const;

Gets the value associated with the keys and converts it to the type `T`.

function: optionally get a value

    template <typename T, typename Otherwise>
    optional<T> get_or( Otherwise&& otherise ) const;

Gets the value associated with the keys and converts it to the type `T`. If it is not of the proper type, it will return a `sol::nullopt` instead.

function: [overloaded] optionally get or create a value

    template <typename T>
    decltype(auto) get_or_create();
    template <typename T, typename Otherwise>
    decltype(auto) get_or_create( Otherwise&& other );

Gets the value associated with the keys if it exists. If it does not, it will set it with the value and return the result.

## `operator[]` proxy-only members

function: valid

    bool valid () const;

Returns whether this proxy actually refers to a valid object. It uses sol::stack::probe_get_field to determine whether or not its valid.

functions: [overloaded] implicit set

    requires( sol::detail::Function<Fx> == false )
    template <typename T>
    proxy& operator=( T&& value );

    requires( sol::detail::Function<Fx> == true )
    template <typename Fx>
    proxy& operator=( Fx&& function );

Sets the value associated with the keys the proxy was generated with to `value`. If this is a function, calls `set_function`. If it is not, just calls `set`. Does not exist on unsage_function_result or protected_function_result.

function: set a callable

    template <typename Fx>
    proxy& set_function( Fx&& fx );

Sets the value associated with the keys the proxy was generated with to a function `fx`. Does not exist on unsafe_function_result or protected_function_result.

function: set a value

    template <typename T>
    proxy& set( T&& value );

Sets the value associated with the keys the proxy was generated with to `value`. Does not exist on unsafe_function_result or protected_function_result.

## stack_proxy

`sol::stack_proxy` is what gets returned by sol::variadic_args and other parts of the framework. It is similar to proxy, but is meant to alias a stack index and not a named variable.

## unsafe_function_result

`unsafe_function_result` is a temporary-only, intermediate-only implicit conversion worker for when function is called. It is NOT meant to be stored or captured with `auto`. It provides fast access to the desired underlying value. It does not implement `set` / `set_function` / templated `operator=`, as is present on proxy.

This type does, however, allow access to multiple underlying values. Use `result.get<Type>(index_offset)` to retrieve an object of `Type` at an offset of `index_offset` in the results. Offset is 0 based. Not specifying an argument defaults the value to 0.

`unsafe_function_result` also has `begin()` and `end()` functions that return (almost) “random-acess” iterators. These return a proxy type that can be implicitly converted to stack_proxy.

## protected_function_result

`protected_function_result` is a nicer version of `unsafe_function_result` that can be used to detect errors. Its gives safe access to the desired underlying value. It does not implement `set` / `set_function` / templated `operator=` as is present on proxy.

This type does, however, allow access to multiple underlying values. Use `result.get<Type>(index_offset)` to retrieve an object of `Type` at an offset of `index_offset` in the results. Offset is 0 based. Not specifying an argument defaults the value to 0.

`unsafe_function_result` also has `begin()` and `end()` functions that return (almost) “random-acess” iterators. These return a proxy type that can be implicitly converted to stack_proxy.

## on function objects and proxies

Note

As of recent versions of sol2 (2.18.2 and above), this is no longer an issue, as even bound classes will have any detectable function call operator automatically bound to the object, to allow this to work without having to use `.set` or `.set_function`. The note here is kept for posterity and information for older versions. There are only some small caveats, see: this note here.