Macros for debugging

Revision en1, by angelbeats, 2020-12-13 07:07:48
namespace std {
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#define tee(...) [](const auto& x){ cerr << "[" << #__VA_ARGS__ << "]: "; debug_out(x); return x; }(__VA_ARGS__)
#else
#define dbg(...) do {} while(0)
#define tee(...) (__VA_ARGS__)
#endif
}

The code above introduces tee, which simply prints one value and returns it.

g++ -DLOCAL -O2 --std=c++17 -o a a.cpp

Don't use c++11. Don't change std to other names. You can get rid of the std wrapper.

And here's the complete version, which handles tuple of any length, as well as complex.

Complete version
Tags macro, c++ 14

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English angelbeats 2020-12-13 07:07:48 2838 Initial revision (published)