From 7739f628cbf8a4b9678c7562daa724a3426e0e6a Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Wed, 13 Nov 2019 21:17:17 +0300 Subject: [PATCH] c++ lambda size test --- lambda_size.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lambda_size.cpp diff --git a/lambda_size.cpp b/lambda_size.cpp new file mode 100644 index 00000000..1139d149 --- /dev/null +++ b/lambda_size.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +#include // for malloc() and free() +using namespace std; + +// replace operator new and delete to log allocations +void* operator new(std::size_t n) +{ + cout << "Allocating " << n << " bytes" << endl; + return malloc(n); +} + +void operator delete(void* p) throw() +{ + free(p); +} + +class test +{ +public: + std::string s; + void a(std::function & f, const char *str) + { + auto l = [this, str]() { cout << str << " ? " << s << " from this\n"; }; + cout << "Assigning lambda3 of size " << sizeof(l) << endl; + f = l; + } +}; + +int main() +{ + std::array arr1; + auto lambda1 = [arr1](){}; + cout << "Assigning lambda1 of size " << sizeof(lambda1) << endl; + std::function f1 = lambda1; + + std::array arr2; + auto lambda2 = [arr2](){}; + cout << "Assigning lambda2 of size " << sizeof(lambda2) << endl; + std::function f2 = lambda2; + + test t; + std::function f3; + t.s = "str"; + t.a(f3, "huyambda"); + f3(); +}