Add examples for using C++ lambdas with chaiscript. #32
This commit is contained in:
@@ -182,6 +182,10 @@ if(BUILD_TESTING)
|
|||||||
target_link_libraries(short_comparison_test ${LIBS} ${EXTRA_LINKER_FLAGS})
|
target_link_libraries(short_comparison_test ${LIBS} ${EXTRA_LINKER_FLAGS})
|
||||||
add_test(NAME short_comparison_test COMMAND short_comparison_test)
|
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
|
add_executable(multifile_test unittests/multifile_test_main.cpp unittests/multifile_test_chai.cpp
|
||||||
unittests/multifile_test_module.cpp)
|
unittests/multifile_test_module.cpp)
|
||||||
|
|||||||
30
unittests/cpp_lambda_test.cpp
Normal file
30
unittests/cpp_lambda_test.cpp
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user