Re: Alternatives to #define?

From: Robbie Hatley (lonewolfintj)
Date: 07/04/04


Date: Sat, 3 Jul 2004 20:05:49 -0700


"Mark A. Gibbs" <x_gibbsmark@rogesr.com_x> wrote:

> just use this.
>
> >>>namespace MyUtils
> >>>{
> >>> inline void doStuff()
> >>> {
> >>> doThis();
> >>> doThat();
> >>> }
> >>>}
> >>
> >>That works, and most C++ purists would recommend it,
> >>but I think it sucks. Too complicated. KISS.
> >>Use the macro instead.
>
> this is probably the most bizarre thing i've read in a while.

Your reading must have been tame lately. :-) Try Anne Rice.

> it works, huh?:
>
> for (int i = 0; i < 10; ++i)
> STUFF
>
> vs.
>
> for (int i = 0; i < 10; ++i)
> doStuff();
>
> "kiss" that.

Well, that's technically correct, but you're losing
style points for no braces.

If you really needed to use such a macro in a
braceless loop like that, it COULD be done, though;
just make a slight correction to the macro definition:

#define DO_STUFF {doThis(); doThat();}

Then this will work:

for (int i = 0; i < 10; ++i)
   DO_STUFF

> ... inline functions were largely created for the
> explicit purpose of eliminating macros ...

Well, for giving programmers an option that will be
better than macros in many cases, at least.
But I wouldn't say "eliminate". Note that the C and
C++ standards committes have not removed macros from
the languages, nor do I think that'll happen.

I agree inline functions are better than macros in
most cases; but if I want to simply paste-in a block
of code in several (or many) places in a program,
I'll sometimes use a macro:

#define PASTE_HUGE_CODE_BLOCK_HERE \
first line of code\
second line of code\
third line of code\
(and so on, for many more lines)\
last line of code

#define PASTE_TINY_CODE_BLOCK_HERE b=7;

int func1(double d)
{
  ...
  PASTE_HUGE_CODE_BLOCK_HERE
  ...
}

float func2(int a, char b)
{
   ...
   #ifdef FLAG_17
   PASTE_HUGE_CODE_BLOCK_HERE
   #else
   PASTE_TINY_CODE_BLOCK_HERE
   #endif
   ...
}

It's really just a matter of using the preprocessor
as an automated compile-time text editor. As long as
one remembers that macros are not functions, but just
cut-n-pastes, the concept works.

-- 
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant


Relevant Pages

  • Re: Alternatives to #define?
    ... "Robbie Hatley" <lonewolfintj at pacbell dot net> wrote in message ... >> namespace MyUtils ... >> inline void doStuff() ... But macros can be difficult to follow too. ...
    (comp.lang.cpp)
  • Re: CERT C Secure Coding Standard - last call for reviewers
    ... The claim that 'macros are dangerous' can, to a certain extent, be ... suggests that inline functions can eliminate this problem. ... the terms of reference of SECCODE, the suggestion to use inline functions ... The example where the interaction of two macros and a file scope object ...
    (comp.lang.c)
  • Re: Cleaning up FILE in stdio..
    ... Macros and __inline functions mean that a significant proportion of ... software compiled on FreeBSD has the existing definition of FILE ... glibc handles this just fine) ... Remove the macros and inlines that poke around inside FILE ...
    (freebsd-arch)
  • Re: C++ as a better C
    ... the overhead of calling the function may be high ... macros have no concept of type safety and can have some nasty ... Inline functions offer many of the benefits of macros, ... int main ...
    (comp.lang.cpp)
  • Re: [PATCH 5/6] i386 virtualization - Make generic set wrprotect a macro
    ... and fixes the build for architectures which include pgtable.h ... >This against the kernel coding style. ... "functions" here are purely macros, ... inline functions for include/asm-i386/pgtable-level.h. ...
    (Linux-Kernel)