aboutsummaryrefslogtreecommitdiffstats
path: root/lib/sol2/examples/source/docs/references_in_lambdas.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sol2/examples/source/docs/references_in_lambdas.cpp')
-rw-r--r--lib/sol2/examples/source/docs/references_in_lambdas.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/sol2/examples/source/docs/references_in_lambdas.cpp b/lib/sol2/examples/source/docs/references_in_lambdas.cpp
new file mode 100644
index 0000000..f681d2a
--- /dev/null
+++ b/lib/sol2/examples/source/docs/references_in_lambdas.cpp
@@ -0,0 +1,33 @@
+#define SOL_ALL_SAFETIES_ON 1
+#include <sol/sol.hpp>
+
+#include <assert.hpp>
+
+int main(int, char*[]) {
+
+ struct test {
+ int blah = 0;
+ };
+
+ test t;
+ sol::state lua;
+ lua.set_function("f", [&t]() {
+ return t;
+ });
+ lua.set_function("g", [&t]() -> test& {
+ return t;
+ });
+
+ lua.script("t1 = f()");
+ lua.script("t2 = g()");
+
+ test& from_lua_t1 = lua["t1"];
+ test& from_lua_t2 = lua["t2"];
+
+ // not the same: 'f' lambda copied
+ c_assert(&from_lua_t1 != &t);
+ // the same: 'g' lambda returned reference
+ c_assert(&from_lua_t2 == &t);
+
+ return 0;
+}