Re: preceding (void) as a do-nothing
On 10 Dez., 03:47, mtall....@gmail.com wrote:
Is the code below valid C++ ? While it compiles under gcc 4.3, I'm
not sure whether "(void)(arg1, arg2)" is a portable way to get the
functionality of "do nothing and ignore all arguments".
#if defined(DO_DEBUG)
#define debug_print do_print
#else
#define debug_print (void)
#endif
inline void do_print(int a, int b)
{
std::cout << a << b << std::endl;
}
int main(int argc, char** argv)
{
debug_print(1,2);
return 0;
}
Except for the missing include's to make the
std::cout definition and corresponding operator<<
overloads available, this program is well-formed.
But I don't think that it realizes in general what
you want. Any expression with side-effects on the
right hand side of debug_print will be evaluated,
before the result is evaluated, just consider the
following adaption of your snippet:
#include <iostream>
#include <ostream>
#if defined(DO_DEBUG)
#define debug_print do_print
#else
#define debug_print (void)
#endif
inline void do_print(int a, int b)
{
std::cout << a << b << std::endl;
}
int get2() {
std::cout << "get2 has been called" << std::endl;
return 2;
}
int main(int argc, char** argv)
{
debug_print(1, get2());
return 0;
}
This will print "get2 has been called" even, even if
DO_DEBUG is not #define'd.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]