Created
December 14, 2017 07:55
-
-
Save TheWisp/26097ee941ce099be33cfe3095df74a6 to your computer and use it in GitHub Desktop.
Debug helper for std::function wrapped lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <functional> | |
template<class F> | |
struct DebuggableLambda : F | |
{ | |
template<class F2> | |
DebuggableLambda(F2&& func, const char* file, int line) | |
: F(std::forward<F2>(func)) | |
, file(file) | |
, line(line) | |
{ | |
} | |
using F::operator(); | |
const char* file; | |
int line; | |
}; | |
template<class F> | |
auto MakeDebuggableLambda(F&& func, const char* file, int line) | |
-> DebuggableLambda<typename std::remove_reference<F>::type> | |
{ | |
return { std::forward<F>(func), file, line }; | |
} | |
#define ENABLE_DEBUG_LAMBDA 1 | |
#if ENABLE_DEBUG_LAMBDA | |
#define DEBUGGABLE_LAMBDA(F)\ | |
MakeDebuggableLambda(F, __FILE__, __LINE__) | |
#else | |
#define DEBUGGABLE_LAMBDA(F) F | |
#endif | |
///////////////////////////////////////////////////////////////////// | |
std::vector<std::function<bool(int)>> testvec; | |
int testfunc() | |
{ | |
testvec.emplace_back([](int x) | |
{ | |
return x > 10000; | |
}); | |
testvec.emplace_back(DEBUGGABLE_LAMBDA([](int x) | |
{ | |
return x % 2 == 0; | |
})); | |
//also works with lvalue lambda | |
int n = 30; | |
auto lambdalvalue = [n](int x) {return x < n; }; | |
testvec.push_back(DEBUGGABLE_LAMBDA(lambdalvalue)); | |
return testvec.size(); | |
} | |
int main() | |
{ | |
int x = testfunc(); | |
return x+1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment