Closed district10 closed 2 years ago
Thank you for the feedback.
This is not supported right now (and I'm not sure if I want to integrate it). But it might be possible to write a macro on your own, similar to:
#define dbg_if(condition, arg) \
if (condition) { \
dbg(arg); \
}
... but with multiple args.
Thank you for the feedback.
This is not supported right now (and I'm not sure if I want to integrate it). But it might be possible to write a macro on your own, similar to:
#define dbg_if(condition, arg) \ if (condition) { \ dbg(arg); \ }
... but with multiple args.
That's simply and smart. It works for me.
Let's just forget about multiple args for now.
(That simple?... Never came to my mind...)
Check this out:
#define DBG_MACRO_NO_WARNING
#include "dbg.h"
#define dbg_if_2(condition, arg1) if (condition) { dbg(arg1); }
#define dbg_if_3(condition, arg1, arg2) if (condition) { dbg(arg1, arg2); }
#define dbg_if_4(condition, arg1, arg2, arg3) if (condition) { dbg(arg1, arg2, arg3); }
// https://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments
#define GET_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define dbg_if(...) GET_MACRO(__VA_ARGS__, dbg_if_4, dbg_if_3, dbg_if_2, UNUSED)(__VA_ARGS__)
int main()
{
std::vector<double> vecs{1, 2, 3};
for (bool verbose: std::vector<bool>{true, false}) {
dbg("--------------");
dbg(verbose);
dbg_if(verbose, vecs);
dbg_if(verbose, vecs, vecs[0]);
dbg_if(verbose, vecs, vecs[0], verbose);
}
dbg("--------------");
}
Output:
[prog.cc:15 (main)] --------------
[prog.cc:16 (main)] verbose = true (bool)
[prog.cc:17 (main)] vecs = {1, 2, 3} (std::vector<double>)
[prog.cc:18 (main)] vecs = {1, 2, 3} (std::vector<double>)
[prog.cc:18 (main)] vecs[0] = 1 (double&)
[prog.cc:19 (main)] vecs = {1, 2, 3} (std::vector<double>)
[prog.cc:19 (main)] vecs[0] = 1 (double&)
[prog.cc:19 (main)] verbose = true (bool)
[prog.cc:15 (main)] --------------
[prog.cc:16 (main)] verbose = false (bool)
[prog.cc:21 (main)] --------------
wanbox link: https://wandbox.org/permlink/PCPQ5xMfIFTPIRT9
I'd like to configure
dbg
to print on some condition.dbg
is a macro so these are not what I'm looking for:Related?: https://github.com/sharkdp/dbg-macro/issues/109
Is it possible to expand the macro on some condition? So I can use it like:
Macros are not expanded at running time, so... Is there any other solutions?
Thank you guys.