0

1.#define debug(...) printf( __VA_ARGS__)

2.#define debug(...) std::cout<< __VA_ARGS__

Apparently, 1 is ok, 2 will get error when compiles. Is there any possibility to use "std::cout" with variable arguments?

What's the point of this macro?

'debug' macro use to print something to debug the code.

void test(const classtype1 &obj1,const classtype2 &obj2)
{
    // rewrite operator<< 
    debug(obj1,obj2); 

    //if use printf, I must call tostring method(or something likes that) to 
    //series the object to string. 
    debug(obj1.tostring(),obj2.tostring());   

    ...
}
zzn
  • 2,376
  • 16
  • 30
  • 1
    What's the point of this macro? – Brian Bi Mar 19 '14 at 03:19
  • 3
    In order to do this, you need C++11, and at that point, why on earth would you choose this over a variadic template? – chris Mar 19 '14 at 03:19
  • If you're using C, use printf. If you're using c++, don't use macros. Even if you need to use va_args, why would you use a macro? – Ben Mar 19 '14 at 03:21
  • because i want to know the function name that calls the macro "debug" , can variadic template do that? – zzn Mar 19 '14 at 03:41
  • first of all what compiler are you using? variadic macros are handled differently by different compilers. – Ahmed Masud Mar 19 '14 at 03:41
  • if there is compiler specify tricks, g++ or clang++ is welcome. – zzn Mar 19 '14 at 03:43
  • Could you use a macro that passes __LINE__ __FUNCTION__ and __VA_ARGS__ to a function. Then in the function loop through the args and use std::cout << ? – thisisdog Mar 19 '14 at 03:50
  • @thisisdog 1.if I use C stdarg(va_start,va_arg...), how do I know the type of every argument? 2. if I use variadic template, I can get the number of arguments with 'sizeof...' , but how do I get argument 'i'(0 – zzn Mar 19 '14 at 04:30

1 Answers1

0

You can do something like:

#define DEBUG(x) do { std::osacquire( std::cerr ) << __FILE__ << ":" << __LINE__ << " " << x << std::endl; } while (0);

And then wherever you want to use the macro:

DEBUG( obj1.tostring() + " some stuff " + obj2.tostring() )
cool_guy2
  • 11
  • 1