1 #include <utility> // for std::forward 2 #include <iostream> 3 4 5 static int total = 0; add(int n)6static void add(int n) 7 { 8 std::cout << "Adding " << n << std::endl; 9 total += n; 10 } 11 display(const char message[])12static void display(const char message[]) 13 { 14 std::cout << message << ": " << total << std::endl; 15 } 16 17 template <class Callable, typename... ArgTypes> Call(Callable native_func,ArgTypes &&...args)18void* Call(Callable native_func, ArgTypes&&... args) noexcept 19 { 20 std::clog << "in Call" << std::endl; 21 return native_func(std::forward<ArgTypes>(args)...); 22 } 23 test_lambda(int delta)24static void* test_lambda(int delta) 25 { 26 std::clog << "in test_lambda" << std::endl; 27 return Call([=](int delta) 28 { 29 std::clog << "in lambda" << std::endl; 30 add(delta); 31 display("total"); 32 return nullptr; 33 }, delta); 34 } 35 main(int argc,char * argv[])36int main(int argc, char* argv[]) 37 { 38 std::clog << "start" << std::endl; 39 test_lambda(5); 40 std::clog << "after first call" << std::endl; 41 test_lambda(20); 42 std::clog << "after second call" << std::endl; 43 test_lambda(-256); 44 std::clog << "after third call" << std::endl; 45 return total != -231; 46 } 47