Topic: Problems with macros && Standard


Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/16
Raw View
Andrew Cox wrote:
>
> what are all those backslashes about?

To put the entire definition on the same line as the #define. They're
needed in any multi-line #definition.

....
> Simone BORDET <simon@most.it> wrote in message
> news:801918$95u$1@nslave1.tin.it...
> > Hi, I'm trying to compile this
> >
> > #define problem(Cnd) \
> > #ifdef _PROBLEM \
> >     _logProblem(Cnd); \
> > #endif

Of course, that's not sufficient to make it legal.


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Paul Grealish" <paul.grealish@uk.geopak-tms.com>
Date: 1999/11/14
Raw View
Simone BORDET wrote in message <801918$95u$1@nslave1.tin.it>...
>Hi, I'm trying to compile this
>
>#define problem(Cnd) \
>#ifdef _PROBLEM \
>    _logProblem(Cnd); \
>#endif
>
>but it seems that is not legal code.
>
>1) Does the standard says that it is really non-legal code ?

Don't know.


>2) How can I define a macro that has conditional compilation code inside ?


You can't.  Or rather, I couldn't manage
it after several hours of trying.

+-------------------------------+
|         Paul Grealish         |
|    Bentley Transportation     |
|      Cambridge, England       |
|   paul.grealish@bentley.com   |
+-------------------------------+
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Hyman Rosen <hymie@prolifics.com>
Date: 1999/11/09
Raw View
Michael Rubenstein <miker3@ix.netcom.com> writes:
> On 7 Nov 1999 04:55:21 -0500, "Simone BORDET" <simon@most.it> wrote:
> >#define problem(Cnd) \
> >#ifdef _PROBLEM \
> >    _logProblem(Cnd); \
> >#endif
> >
> This code is completely legal.  If your compiler does not
> allow it, then your compiler is broken.

No. This code is illegal. See 16.3.2/1. Each # operator within
a function-like macro must be followed by a macro parameter.


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Simone BORDET" <simon@most.it>
Date: 1999/11/07
Raw View
Hi, I'm trying to compile this

#define problem(Cnd) \
#ifdef _PROBLEM \
    _logProblem(Cnd); \
#endif

but it seems that is not legal code.

1) Does the standard says that it is really non-legal code ?
2) How can I define a macro that has conditional compilation code inside ?

TIA

Simon
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Phlip" <tegan@deltanet.com>
Date: 1999/11/07
Raw View
Simone BORDET wrote:

> Hi, I'm trying to compile this
>
> #define problem(Cnd) \
> #ifdef _PROBLEM \
>     _logProblem(Cnd); \
> #endif
>
> but it seems that is not legal code.
>
> 1) Does the standard says that it is really non-legal code ?

Yup. And the main reason is the combination of A> there's no reason
to do it, and B> permitting the character sequence \ \n # to not
start a preprocessor macro whilst \n # still does would wreak havoc
with the parsing rules.

> 2) How can I define a macro that has conditional compilation code
inside ?

Easy - put it on the outside:

 #ifdef _PROBLEM
 # define problem(Cnd)     _logProblem(Cnd);
 #else
 # define problem(Cnd)
 #endif

Two more points. _ on the beginning of an identifier is reserved for
everyone except you. I always put macros into a simulated namespace
by putting _ on the end.

And that ; on the end of the line is distasteful, and dangerous if
you put the macro some place where something's counting the ; , like
a 'for' header.

--
 Phlip at politizen dot com                  (address munged)
======= http://users.deltanet.com/~tegan/home.html =======
  --  Please state the nature of the programming emergency  --



      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/07
Raw View
Simone BORDET wrote:
>
> Hi, I'm trying to compile this
>
> #define problem(Cnd) \
> #ifdef _PROBLEM \
>     _logProblem(Cnd); \
> #endif
>
> but it seems that is not legal code.
>
> 1) Does the standard says that it is really non-legal code ?

Yes. Specifically, the '\' at the end of each line causes the new-line
character following it to be dropped during phase 2 of translation. In
phase 4, when pre-processing commands are recognized and evaluated,
they're each required to start on a new line, but by that point the
whole thing has been merged into a single line. The '#ifdef' and
'#endif' gets passed on without being evaluated, and gets rejected
during phase 7 as invalid.

> 2) How can I define a macro that has conditional compilation code inside ?

You can't. You do have several alternative ways to handle it, however:

 #ifdef _PROBLEM
 #define problem(Cnd) _logProblem(Cnd);
 #else
 #define problem(Cnd)
 #endif

 template<class parameter_type>
 // Depending upon what _logProblem is, the template might not
 // be needed
 inline void problem(parameter_type Cnd)
 {
 #ifdef _PROBLEM
  _logProblem(Cnd);
 #endif
 }
 // If _logProblem is also a macro, the above approach
 // might not work.

 #define problem(Cnd) (_PROBLEM ? (_logProblem(Cnd)) \
  : appropriate_alternative)
 // the '()' around _logProblem() are needed only if it's a
 // macro.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Pete Becker <petebecker@acm.org>
Date: 1999/11/07
Raw View
Simone BORDET wrote:
>
> Hi, I'm trying to compile this
>
> #define problem(Cnd) \
> #ifdef _PROBLEM \
>     _logProblem(Cnd); \
> #endif
>
> but it seems that is not legal code.
>
> 1) Does the standard says that it is really non-legal code ?
> 2) How can I define a macro that has conditional compilation code inside ?
>

You can't. Write it inside out:

#ifdef _PROBLEM
#define problem(Cnd) _logProblem(Cnd);
#endif

--
Pete Becker
Dinkumware, Ltd.
http://www.dinkumware.com

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "John Harrison" <jahhaj@dtn.ntl.com>
Date: 1999/11/07
Raw View

Simone BORDET <simon@most.it> wrote in message
news:801918$95u$1@nslave1.tin.it...
> Hi, I'm trying to compile this
>
> #define problem(Cnd) \
> #ifdef _PROBLEM \
>     _logProblem(Cnd); \
> #endif
>
> but it seems that is not legal code.
>
> 1) Does the standard says that it is really non-legal code ?

Yes

> 2) How can I define a macro that has conditional compilation code inside ?
>

You can't. You'd be better off with something like

#ifdef _PROBLEM
#define problem(Cnd)    _logProblem(Cnd)
#else
#define problem(Cnd)
#endif

John




      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/11/07
Raw View
Simone BORDET wrote:

> Hi, I'm trying to compile this
>
> #define problem(Cnd) \
> #ifdef _PROBLEM \
>     _logProblem(Cnd); \
> #endif
>
> but it seems that is not legal code.
>
> 1) Does the standard says that it is really non-legal code ?

No, the result of macro expansion isn't preprocessed. And anyway,
it would produce a one-line conditional:

#ifdef _PROBLEM _logProblem(Cnd); #endif

not

#ifdef _PROBLEM
    _logProblem(Cnd);
#endif

> 2) How can I define a macro that has conditional compilation code inside ?

Use a good preprocessor; <TROLL>that is, anything but cpp</TROLL>

--

Valentin Bonnard

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrew Cox" <andrewwiggin@hotmail.com>
Date: 1999/11/08
Raw View
what are all those backslashes about?

yes of course it is illegal code by the standard

Simone BORDET <simon@most.it> wrote in message
news:801918$95u$1@nslave1.tin.it...
> Hi, I'm trying to compile this
>
> #define problem(Cnd) \
> #ifdef _PROBLEM \
>     _logProblem(Cnd); \
> #endif
>
> but it seems that is not legal code.
>
> 1) Does the standard says that it is really non-legal code ?
> 2) How can I define a macro that has conditional compilation code inside ?

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/08
Raw View
Pete Becker wrote:
>
> Simone BORDET wrote:
> >
> > Hi, I'm trying to compile this
> >
> > #define problem(Cnd) \
> > #ifdef _PROBLEM \
> >     _logProblem(Cnd); \
> > #endif
> >
> > but it seems that is not legal code.
> >
> > 1) Does the standard says that it is really non-legal code ?
> > 2) How can I define a macro that has conditional compilation code inside ?
> >
>
> You can't. Write it inside out:
>
> #ifdef _PROBLEM
> #define problem(Cnd)    _logProblem(Cnd);
> #endif

That doesn't quite do the job; you need an #else clause that gives
problem() an empty definition as well.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Michael Rubenstein <miker3@ix.netcom.com>
Date: 1999/11/08
Raw View
On 7 Nov 1999 04:55:21 -0500, "Simone BORDET" <simon@most.it>
wrote:

>Hi, I'm trying to compile this
>
>#define problem(Cnd) \
>#ifdef _PROBLEM \
>    _logProblem(Cnd); \
>#endif
>
>but it seems that is not legal code.
>
>1) Does the standard says that it is really non-legal code ?
>2) How can I define a macro that has conditional compilation code inside ?

No.  This code is completely legal.  If your compiler does not
allow it, then your compiler is broken.

Of course, it is illegal to use the macro problem with this
definition.

In C++ (or C) you cannot define a macro that contains
preprocessor directives.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]