 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Konrad Guest
|
Posted: Sun Feb 26, 2006 6:06 pm Post subject: #defines and multiline expressions |
|
|
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. |
|
| Back to top |
|
 |
David Lindauer Guest
|
Posted: Sun Feb 26, 2006 7:06 pm Post subject: Re: #defines and multiline expressions |
|
|
Konrad wrote:
| Quote: | 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... |
|
| Back to top |
|
 |
Konrad Guest
|
Posted: Sun Feb 26, 2006 8:06 pm Post subject: Re: #defines and multiline expressions |
|
|
David Lindauer wrote:
| Quote: |
Konrad wrote:
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...
|
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. |
|
| Back to top |
|
 |
TB Guest
|
Posted: Sun Feb 26, 2006 8:06 pm Post subject: Re: #defines and multiline expressions |
|
|
David Lindauer skrev:
| Quote: |
Konrad wrote:
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...
|
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;
}
--
TB @ SWEDEN |
|
| Back to top |
|
 |
Marc Thrun Guest
|
Posted: Sun Feb 26, 2006 9:06 pm Post subject: Re: #defines and multiline expressions |
|
|
TB wrote:
| Quote: | David Lindauer skrev:
snip
#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;
}
|
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. |
|
| Back to top |
|
 |
Asfand Yar Qazi Guest
|
Posted: Sun Feb 26, 2006 11:06 pm Post subject: Re: #defines and multiline expressions |
|
|
Marc Thrun wrote:
| Quote: | TB wrote:
David Lindauer skrev:
snip
#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;
}
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...
--
To reply, take of all ZIGs !!
Alternative email address: emailZIG (AT) asfandyarZIG (DOT) cjbZIG.net |
|
| Back to top |
|
 |
Asfand Yar Qazi Guest
|
Posted: Sun Feb 26, 2006 11:06 pm Post subject: Re: #defines and multiline expressions |
|
|
Asfand Yar Qazi wrote:
| Quote: | Marc Thrun wrote:
TB wrote:
David Lindauer skrev:
snip
#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;
}
|
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 :-)
--
To reply, take of all ZIGs !!
Alternative email address: emailZIG (AT) asfandyarZIG (DOT) cjbZIG.net |
|
| Back to top |
|
 |
Joe Hotchkiss Guest
|
Posted: Mon Feb 27, 2006 9:06 am Post subject: Re: #defines and multiline expressions |
|
|
"Konrad" <konradp_removit_ (AT) wp (DOT) pl> wrote in message
news:dtspjf$g69$1 (AT) nemesis (DOT) news.tpi.pl...
| Quote: | 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 |
|
| Back to top |
|
 |
Konrad Guest
|
Posted: Mon Feb 27, 2006 7:06 pm Post subject: Re: #defines and multiline expressions |
|
|
(..)
OK, I got it now, thank you all!
| Quote: | 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  |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|