Skip to content

Instantly share code, notes, and snippets.

@TheWisp
Created December 14, 2017 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheWisp/26097ee941ce099be33cfe3095df74a6 to your computer and use it in GitHub Desktop.
Save TheWisp/26097ee941ce099be33cfe3095df74a6 to your computer and use it in GitHub Desktop.
Debug helper for std::function wrapped lambda
#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