#defines and multiline expressions

K

Konrad

Hello,
Stupid question, but I just don't remember, here is what I want to do:

#ifdef _UNICODE
#define ONLY_UNICODE(t) t
#else
#define ONLY_UNICODE(t)
#endif

You know what it does, but I'm not sure, if I actually can use #defines
with multiline code? I'm just not sure, here is an example of using this:

ONLY_UNICODE
(
MessageBox(_T("A lot of lines"));
MessageBox(_T("are included"));
MessageBox(_T("as a paremeter of this directive"));
);

It does compile, but maybe I should be worry about it ? I do remember
that there was something about including more than one line as a
#define's parameter, I somehow don't remember what was that.
 
D

David Lindauer

Konrad said:
Hello,
Stupid question, but I just don't remember, here is what I want to do:

#ifdef _UNICODE
#define ONLY_UNICODE(t) t
#else
#define ONLY_UNICODE(t)
#endif

You know what it does, but I'm not sure, if I actually can use #defines
with multiline code? I'm just not sure, here is an example of using this:

ONLY_UNICODE
(
MessageBox(_T("A lot of lines"));
MessageBox(_T("are included"));
MessageBox(_T("as a paremeter of this directive"));
);

It does compile, but maybe I should be worry about it ? I do remember
that there was something about including more than one line as a
#define's parameter, I somehow don't remember what was that.

#define ONLY_UNICODE( \
MessageBox(_T("A lot of lines")); \
MessageBox(_T("are included")); \
MessageBox(_T("as a paremeter of this directive")); \
);

might work... the '\' means concatenate two lines together...
 
K

Konrad

David said:
Konrad wrote:




#define ONLY_UNICODE( \
MessageBox(_T("A lot of lines")); \
MessageBox(_T("are included")); \
MessageBox(_T("as a paremeter of this directive")); \
);

might work... the '\' means concatenate two lines together...

Thanks, but it's not what I meant, I was not clear enough I :)
the "ONLY_UNICODE" is defined OK, it is how it is supposed to be, the
question is: Can I _use_ it as in my example, that is - can I pass
multiline parameter to it, I know it compiles, I just wanted to know if
there are things to be worry about.
 
T

TB

David Lindauer skrev:
#define ONLY_UNICODE( \
MessageBox(_T("A lot of lines")); \
MessageBox(_T("are included")); \
MessageBox(_T("as a paremeter of this directive")); \
);

might work... the '\' means concatenate two lines together...

I don't think that was what he was asking. He wondered if the macro
argument could span multiple lines. And the answer is yes, with the
exception that possible commas will interfere.

#define MACRO(C) C

int main() {
MACRO
(
int x;
x = 3;
)
return 0;
}

But this doesn't compile:

#define MACRO(C) C

int main() {
MACRO
(
int x,y;
x = 3;
)
return 0;
}
 
M

Marc Thrun

TB said:
David Lindauer skrev:


I don't think that was what he was asking. He wondered if the macro
argument could span multiple lines. And the answer is yes, with the
exception that possible commas will interfere.

#define MACRO(C) C

int main() {
MACRO
(
int x;
x = 3;
)
return 0;
}

But this doesn't compile:

#define MACRO(C) C

int main() {
MACRO
(
int x,y;
x = 3;
)
return 0;
}

I would try something like

#ifdef _UNICODE
#define ONLY_UNICODE
#else
#define ONLY_UNICODE while(0)
#endif

int main()
{
ONLY_UNICODE
{
/* code */
}
return 0;
}

This has no problems with commas and a compiler will most likely
optimize the while(0) loop away anyway.
 
A

Asfand Yar Qazi

Marc said:
I would try something like

#ifdef _UNICODE
#define ONLY_UNICODE
#else
#define ONLY_UNICODE while(0)
#endif

int main()
{
ONLY_UNICODE
{
/* code */
}
return 0;
}

This has no problems with commas and a compiler will most likely
optimize the while(0) loop away anyway.

You can have multi-line arguments to argument-taking macros?!?! Bloody hell,
I never knew that!

This could revolutionise something I was stuck on in one of my projects...
 
A

Asfand Yar Qazi

I must say that I've bettered myself. Try the following code:

#define MACRO(...) __VA_ARGS__

int main() {
MACRO
(
int x,y;
x = 3;
)
}

Note: only works in C99 mode. Worked alright for me on GCC (using g++
front-end as well), don't know about other compilers. But, we're getting
there :)
 
J

Joe Hotchkiss

Konrad said:
Hello,
Stupid question, but I just don't remember, here is what I want to do:

#ifdef _UNICODE
#define ONLY_UNICODE(t) t
#else
#define ONLY_UNICODE(t)
#endif

You know what it does, but I'm not sure, if I actually can use #defines
with multiline code? I'm just not sure, here is an example of using this:

ONLY_UNICODE
(
MessageBox(_T("A lot of lines"));
MessageBox(_T("are included"));
MessageBox(_T("as a paremeter of this directive"));
);

What's wrong with
#ifdef _UNICODE
MessageBox(_T("A lot of lines"));
MessageBox(_T("are included"));
MessageBox(_T("as a paremeter of this directive"));
#endif

--
Regards,

Joe Hotchkiss,
http://joe.hotchkiss.com
http://harrowsubaqua.org.uk

XXXXXXXXXXXXXXXXXXXXXXXXX
X joe.hotchkiss X
X at selex-sas.com X
XXXXXXXXXXXXXXXXXXXXXXXXX
 
K

Konrad

(..)

OK, I got it now, thank you all!
>What's wrong with
>#ifdef _UNICODE
> MessageBox(_T("A lot of lines"));
> MessageBox(_T("are included"));
> MessageBox(_T("as a paremeter of this directive"));
>#endif

Mine is shorter :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top