I’m basically looking for a way to automate typing stuff like the following:
cout << 'a[' << x << '][' << y << '] =\t' << a[x][y] << endl;
Something like:
PRINTDBG(a[x][y]);
Ideally this would also work for
PRINTDBG(func(arg1, arg2));
and even
PRINTDBG(if(condition) func(foo););
(which would print e.g. ‘if(false) func(5)’).
Nonportable hacks welcome too 🙂
(no, using a debugger isn’t the same, it’s much less flexible and I find it confusing)
This is, in the way you want it, not possible. If you have if(condition) func(foo); given to a macro, it can stringize that stuff, and it will print if(condition) func(foo);, but not with the actual values of the variables substituted. Remember the preprocessor doesn’t know about the structure about that code.
For debugging, i would use some type-safe printf variant like boost.format or some home brew printf with boost.fusion, which make the job of printing stuff like that much more easy: