How do i do variable arguments with macros for Visual C/C++ 6?
(I'm working on a C console app)

for example, this wont compile:

const int DEBUG=0;
#define printd(fmt...) (if(DEBUG) {printf(fmt);}else{})

neither will this:
#define printd(fmt, ...)

both cause compiler to complain about '.' error

Thanks
Eric

Re: need help with variable args prep. macro by Alex

Alex
Fri Jul 11 01:29:31 CDT 2008

"Eric" wrote:
> How do i do variable arguments with macros for Visual C/C++ 6?

VC++ 6.0 doesn't support variadic macros. It is too old.

Alex



Re: need help with variable args prep. macro by Norbert

Norbert
Fri Jul 11 12:04:30 CDT 2008



Eric schrieb:
> How do i do variable arguments with macros for Visual C/C++ 6?
> (I'm working on a C console app)
>
> for example, this wont compile:
>
> const int DEBUG=0;
> #define printd(fmt...) (if(DEBUG) {printf(fmt);}else{})
>
> neither will this:
> #define printd(fmt, ...)
>
> both cause compiler to complain about '.' error

You don't.
You could use inline functions instead, or use some kind of workaround with
double braces, like:

#define PRINTD(format) if (DEBUG) printf format

and call it with double braces like this:

PRINTD(("%s %d\n", "Hello", 10));

By the way, do not use a name of DEBUG for the debug variable. Depending on the
headers you include, it might be #defined somewhere in the system headers for
debug builds.

Norbert

Re: need help with variable args prep. macro by Hendrik

Hendrik
Fri Jul 11 13:37:03 CDT 2008

Norbert Unterberg <nunterberg@newsgroups.nospam> wrote:
> Eric schrieb:
>> How do i do variable arguments with macros for Visual C/C++ 6?
> [...]
>
> You don't.
> You could use inline functions instead, or use some kind of workaround with
> double braces, like:
>
> #define PRINTD(format) if (DEBUG) printf format
>
> and call it with double braces like this:
>
> PRINTD(("%s %d\n", "Hello", 10));

Mhmm. If you use macros anyway, why not define them this
way
#define PRINTD if (DEBUG) printf
so you don't need those extra braces?

> By the way, do not use a name of DEBUG for the debug variable. Depending on the
> headers you include, it might be #defined somewhere in the system headers for
> debug builds.

Ack.

> Norbert

Schobi

--
SpamTrap@gmx.de is never read
I'm HSchober at gmx dot de
"I guess at some point idealism meets human nature and
explodes." Daniel Orner



Re: need help with variable args prep. macro by Tim

Tim
Sun Jul 13 23:33:17 CDT 2008

"Hendrik Schober" <SpamTrap@gmx.de> wrote:
>Norbert Unterberg <nunterberg@newsgroups.nospam> wrote:
>
>> You could use inline functions instead, or use some kind of workaround with
>> double braces, like:
>>
>> #define PRINTD(format) if (DEBUG) printf format
>>
>> and call it with double braces like this:
>>
>> PRINTD(("%s %d\n", "Hello", 10));
>
> Mhmm. If you use macros anyway, why not define them this
> way
> #define PRINTD if (DEBUG) printf
> so you don't need those extra braces?

OK, but then what do you do for the non-debug case, where you want the
expression to go away?

There are certainly ways to do that:
#define PRINTD 1?0:
but I'm not sure they are quite as elegant.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: need help with variable args prep. macro by Alex

Alex
Mon Jul 14 00:43:51 CDT 2008

"Tim Roberts" wrote:
> OK, but then what do you do for the non-debug case, where you
> want the expression to go away?

I think that the easiest way is to define a function:

void DebugPrint(LPCTSTR pszFormat, ...);

#if defined(NDEBUG)
# define PRINTD ((void*)0)
#else
# define PRINTD DebugPrint
#endif // NDEBUG

The above macro will expant to a fuunction call in debug mode and
to nothing in release mode.

Alex



Re: need help with variable args prep. macro by Tim

Tim
Wed Jul 16 00:12:54 CDT 2008

"Alex Blekhman" <tkfx.REMOVE@yahoo.com> wrote:

>"Tim Roberts" wrote:
>> OK, but then what do you do for the non-debug case, where you
>> want the expression to go away?
>
>I think that the easiest way is to define a function:
>
>void DebugPrint(LPCTSTR pszFormat, ...);
>
>#if defined(NDEBUG)
># define PRINTD ((void*)0)
>#else
># define PRINTD DebugPrint
>#endif // NDEBUG
>
>The above macro will expant to a fuunction call in debug mode and
>to nothing in release mode.

No, it expands to a syntax error in release mode (term does not evaluate to
a function accepting N arguments). I can't guess what you were going for
there, but that's not it.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Re: need help with variable args prep. macro by Alex

Alex
Wed Jul 16 01:39:57 CDT 2008

"Tim Roberts" wrote:
> No, it expands to a syntax error in release mode (term does not
> evaluate to a function accepting N arguments). I can't guess
> what you were going for there, but that's not it.

My bad. You are right. I confused it with a macro that accepts one
parameter. The4 corret solution is to use your variant or to
define `DebugPrint' function with an empty body for release
builds.

I don't have VC++6.0 at hand anomore to check their implementation
of TRACE macro. They did something similar to the above.
Fortunately, for later versions of VC++ MS introduced `__noop'. I
am not sure whether it is available in VC++6.0 though.

Alex



Re: need help with variable args prep. macro by Murrgon

Murrgon
Wed Jul 16 09:26:41 CDT 2008

Alex Blekhman wrote:
> I don't have VC++6.0 at hand anomore to check their implementation
> of TRACE macro. They did something similar to the above.
> Fortunately, for later versions of VC++ MS introduced `__noop'. I
> am not sure whether it is available in VC++6.0 though.

I believe this is what you're looking for:

#if defined(NDEBUG)
# define PRINTD 1 ? (void)0 : (void)
#else
# define PRINTD DebugPrint
#endif // NDEBUG

Murrgon

Re: need help with variable args prep. macro by Alex

Alex
Wed Jul 16 10:04:13 CDT 2008

"Murrgon" wrote:
> I believe this is what you're looking for:
>
> #if defined(NDEBUG)
> # define PRINTD 1 ? (void)0 : (void)
> #else
> # define PRINTD DebugPrint
> #endif // NDEBUG

Actually, this is the same trick as Tim Roberts suggested in his
other post.

Alex