Add examples for using C++ lambdas with chaiscript. #32

This commit is contained in:
Jason Turner
2012-05-16 15:55:03 -06:00
parent bca86c87e1
commit 68df78a2a6
2 changed files with 34 additions and 0 deletions

View File

@@ -182,6 +182,10 @@ if(BUILD_TESTING)
target_link_libraries(short_comparison_test ${LIBS} ${EXTRA_LINKER_FLAGS})
add_test(NAME short_comparison_test COMMAND short_comparison_test)
add_executable(cpp_lambda_test unittests/cpp_lambda_test.cpp)
target_link_libraries(cpp_lambda_test ${LIBS} ${EXTRA_LINKER_FLAGS})
add_test(NAME cpp_lambda_test COMMAND cpp_lambda_test)
add_executable(multifile_test unittests/multifile_test_main.cpp unittests/multifile_test_chai.cpp
unittests/multifile_test_module.cpp)

View File

@@ -0,0 +1,30 @@
#include <chaiscript/utility/utility.hpp>
#include <chaiscript/chaiscript_stdlib.hpp>
int main()
{
// We cannot deduce the type of a lambda expression, you must either wrap it
// in an std::function or provide the signature
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());
// provide the signature
chai.add(chaiscript::fun<std::string ()>([] { return "hello"; } ), "f1");
// wrap
chai.add(chaiscript::fun(std::function<std::string ()>([] { return "world"; } )), "f2");
if (chai.eval<std::string>("f1()") == "hello"
&& chai.eval<std::string>("f2()") == "world")
{
return EXIT_SUCCESS;
} else {
return EXIT_FAILURE;
}
}