A silly belief shattered (happily)

Discussion in 'C++' started by Alf P. Steinbach, Jul 28, 2009.

  1. For as long as I can remember I have, incorrectly, believed that

    MACRO ( whatever )

    would not be a valid macro invocation.

    Of course the space before "(" wreaks havoc with a macro-with-args /definition/,
    and that may be where I got the impression from.

    Anyway, with the space allowed much is simplified...


    Cheers,

    - Alf (amazed at the macro trickery in Boost Preprocessor)
     
    Alf P. Steinbach, Jul 28, 2009
    #1
    1. Advertising

  2. Alf P. Steinbach wrote:
    > For as long as I can remember I have, incorrectly, believed that
    >
    > MACRO ( whatever )
    >
    > would not be a valid macro invocation.
    >
    > Of course the space before "(" wreaks havoc with a macro-with-args
    > /definition/, and that may be where I got the impression from.
    >
    > Anyway, with the space allowed much is simplified...


    For example?
     
    Vladimir Jovic, Jul 28, 2009
    #2
    1. Advertising

  3. * Vladimir Jovic:
    > Alf P. Steinbach wrote:
    >> For as long as I can remember I have, incorrectly, believed that
    >>
    >> MACRO ( whatever )
    >>
    >> would not be a valid macro invocation.
    >>
    >> Of course the space before "(" wreaks havoc with a macro-with-args
    >> /definition/, and that may be where I got the impression from.
    >>
    >> Anyway, with the space allowed much is simplified...

    >
    > For example?


    #define FIRST( a, b, c ) a

    #define BLAHBLAH( tuple ) FIRST tuple

    int main()
    {
    BLAHBLAH( (42, 41, 43) ); // 42
    }


    Cheers,

    - Alf
     
    Alf P. Steinbach, Jul 28, 2009
    #3
  4. Alf P. Steinbach wrote:
    > * Vladimir Jovic:
    >> Alf P. Steinbach wrote:
    >>> For as long as I can remember I have, incorrectly, believed that
    >>>
    >>> MACRO ( whatever )
    >>>
    >>> would not be a valid macro invocation.
    >>>
    >>> Of course the space before "(" wreaks havoc with a macro-with-args
    >>> /definition/, and that may be where I got the impression from.
    >>>
    >>> Anyway, with the space allowed much is simplified...

    >>
    >> For example?

    >
    > #define FIRST( a, b, c ) a
    >
    > #define BLAHBLAH( tuple ) FIRST tuple
    >
    > int main()
    > {
    > BLAHBLAH( (42, 41, 43) ); // 42
    > }


    Ok, so this:
    BLAHBLAH ( 42, 41, 43 ); // 42
    is causing a compilation error, but this:
    BLAHBLAH ( (42, 41, 43) ); // 42
    is not.

    Is that what you are saying?
     
    Vladimir Jovic, Jul 28, 2009
    #4
  5. Alf P. Steinbach

    Ian Collins Guest

    Vladimir Jovic wrote:
    > Alf P. Steinbach wrote:
    >> * Vladimir Jovic:
    >>> Alf P. Steinbach wrote:
    >>>> For as long as I can remember I have, incorrectly, believed that
    >>>>
    >>>> MACRO ( whatever )
    >>>>
    >>>> would not be a valid macro invocation.
    >>>>
    >>>> Of course the space before "(" wreaks havoc with a macro-with-args
    >>>> /definition/, and that may be where I got the impression from.
    >>>>
    >>>> Anyway, with the space allowed much is simplified...
    >>>
    >>> For example?

    >>
    >> #define FIRST( a, b, c ) a
    >>
    >> #define BLAHBLAH( tuple ) FIRST tuple
    >>
    >> int main()
    >> {
    >> BLAHBLAH( (42, 41, 43) ); // 42
    >> }

    >
    > Ok, so this:
    > BLAHBLAH ( 42, 41, 43 ); // 42
    > is causing a compilation error, but this:
    > BLAHBLAH ( (42, 41, 43) ); // 42
    > is not.
    >
    > Is that what you are saying?


    The key is

    FIRST (42, 41, 43)

    works.

    --
    Ian Collins
     
    Ian Collins, Jul 28, 2009
    #5
  6. * Vladimir Jovic:
    > Alf P. Steinbach wrote:
    >> * Vladimir Jovic:
    >>> Alf P. Steinbach wrote:
    >>>> For as long as I can remember I have, incorrectly, believed that
    >>>>
    >>>> MACRO ( whatever )
    >>>>
    >>>> would not be a valid macro invocation.
    >>>>
    >>>> Of course the space before "(" wreaks havoc with a macro-with-args
    >>>> /definition/, and that may be where I got the impression from.
    >>>>
    >>>> Anyway, with the space allowed much is simplified...
    >>>
    >>> For example?

    >>
    >> #define FIRST( a, b, c ) a
    >>
    >> #define BLAHBLAH( tuple ) FIRST tuple
    >>
    >> int main()
    >> {
    >> BLAHBLAH( (42, 41, 43) ); // 42
    >> }

    >
    > Ok, so this:
    > BLAHBLAH ( 42, 41, 43 ); // 42
    > is causing a compilation error, but this:
    > BLAHBLAH ( (42, 41, 43) ); // 42
    > is not.
    >
    > Is that what you are saying?


    No, I'm saying that because the space is allowed one can easily let macros
    produce other macro invocations, where the name of the macro to be invoked is a
    parameter (otherwise, token-pasting via ## doesn't produce a valid token).

    I've not done much macro stuff (obviously!).

    But now I need some facility to simplify some notation, and I'm amazed at what's
    possible using just the primitive C and C++ preprocessor.


    Cheers,

    - Alf
     
    Alf P. Steinbach, Jul 28, 2009
    #6
  7. Alf P. Steinbach

    Puppet_Sock Guest

    On Jul 28, 4:35 am, "Alf P. Steinbach" <> wrote:
    [snip]
    > No, I'm saying that because the space is allowed one can easily let macros
    > produce other macro invocations, where the name of the macro to be invoked is a
    > parameter (otherwise, token-pasting via ## doesn't produce a valid token)..


    Um. If I was in a code review, and I found such
    a thing, I'd flag it as a code fault even if it
    did "work."

    > I've not done much macro stuff (obviously!).


    This is a *good* thing.

    > But now I need some facility to simplify some notation, and I'm amazed at what's
    > possible using just the primitive C and C++ preprocessor.


    I think there are better ways to simplify notation
    than using macros the way you describe.

    Indeed, I'm not real sure I'd say that this scheme
    is aptly described as "simplifying" notation.
    Socks
     
    Puppet_Sock, Jul 29, 2009
    #7
  8. * Puppet_Sock:
    > On Jul 28, 4:35 am, "Alf P. Steinbach" <> wrote:
    > [snip]
    >> No, I'm saying that because the space is allowed one can easily let macros
    >> produce other macro invocations, where the name of the macro to be invoked is a
    >> parameter (otherwise, token-pasting via ## doesn't produce a valid token).

    >
    > Um. If I was in a code review, and I found such
    > a thing, I'd flag it as a code fault even if it
    > did "work."


    You'd have to flag the Boost Preprocessor library as a "code fault", then. ;-)

    So as a general statement it makes no sense, but it depends on the context.

    For example, in a sweatshop-like workplace it can make sense to impose literal
    rules, because the grunt workers are just robots, or would be if such existed.
    In other contexts the programmer's job is to apply intelligence and aesthetics,
    something that robots following mechanical code-production rules would find
    difficult or impossible to do. The application of intelligence and aestethics
    can't be judged by literal rules, or rather, should ideally not be so judged,
    for doing that will in most cases lower quality instead of raising it (the few
    cases where it might raise quality being those where the rules are
    coincidentally "right" *and* the programmer had a lapse of judgment).


    >> I've not done much macro stuff (obviously!).

    >
    > This is a *good* thing.


    :)

    I think I agree.


    >> But now I need some facility to simplify some notation, and I'm amazed at what's
    >> possible using just the primitive C and C++ preprocessor.

    >
    > I think there are better ways to simplify notation
    > than using macros the way you describe.
    >
    > Indeed, I'm not real sure I'd say that this scheme
    > is aptly described as "simplifying" notation.


    It's good that you write you're not "real sure", since you don't know anything
    about "this scheme". :)


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Jul 29, 2009
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don

    Silly question....

    Don, Sep 26, 2003, in forum: VHDL
    Replies:
    2
    Views:
    498
  2. John
    Replies:
    2
    Views:
    495
  3. Homa
    Replies:
    7
    Views:
    465
    John Saunders
    Nov 16, 2003
  4. John

    silly question

    John, Jul 20, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    403
  5. Rick Morayniss

    Frustrated beyond belief

    Rick Morayniss, Oct 21, 2004, in forum: ASP General
    Replies:
    5
    Views:
    151
    Chris Hohmann
    Oct 22, 2004
Loading...

Share This Page