Preprocessor concatenation of operator and ...

Discussion in 'C++' started by mrstephengross, Oct 11, 2005.

  1. I'm using gcc 3.3.1 to compile the following code (below). I've written
    a macro to simplify writing operators. The macro uses the '##' operator
    to paste together 'operator' and the name of the operator (ie: '+').
    gcc reports that pasting the two things together "does not give a valid
    preprocessing token". Here's the code:

    ====================
    #define OPER(op) operator ## op

    class thing
    {
    public:

    bool OPER(==) (const thing & other) const { return true; }
    };


    int main() { return 0; }
    =============================

    The error reads: (line 7): pasting "operator" and "==" does not give a
    valid preprocessing token

    Any ideas?

    Thanks,
    --Steve ()
    mrstephengross, Oct 11, 2005
    #1
    1. Advertising

  2. mrstephengross

    Dan Cernat Guest

    mrstephengross wrote:
    > I'm using gcc 3.3.1 to compile the following code (below). I've written
    > a macro to simplify writing operators. The macro uses the '##' operator
    > to paste together 'operator' and the name of the operator (ie: '+').
    > gcc reports that pasting the two things together "does not give a valid
    > preprocessing token". Here's the code:
    >
    > ====================
    > #define OPER(op) operator ## op
    >
    > class thing
    > {
    > public:
    >
    > bool OPER(==) (const thing & other) const { return true; }
    > };
    >
    >
    > int main() { return 0; }
    > =============================
    >
    > The error reads: (line 7): pasting "operator" and "==" does not give a
    > valid preprocessing token
    >
    > Any ideas?
    >
    > Thanks,
    > --Steve ()


    Compiles fine with VC 6.0 and 7.1

    /dan
    Dan Cernat, Oct 11, 2005
    #2
    1. Advertising

  3. mrstephengross

    Maett Guest

    mrstephengross wrote:

    > I'm using gcc 3.3.1 to compile the following code (below). I've written
    > a macro to simplify writing operators. The macro uses the '##' operator
    > to paste together 'operator' and the name of the operator (ie: '+').
    > gcc reports that pasting the two things together "does not give a valid
    > preprocessing token". Here's the code:
    >
    > ====================
    > #define OPER(op) operator ## op
    >
    > class thing
    > {
    > public:
    >
    > bool OPER(==) (const thing & other) const { return true; }
    > };
    >
    >
    > int main() { return 0; }
    > =============================
    >
    > The error reads: (line 7): pasting "operator" and "==" does not give a
    > valid preprocessing token
    >
    > Any ideas?
    >
    > Thanks,
    > --Steve ()
    >
    >


    With gcc 3.4.3 it compiles ok if I omit the ##:
    #define OPER(op) operator op

    Maett
    Maett, Oct 11, 2005
    #3
  4. Aha! Ok, so that works. That certainly solves my problem; do you have
    any idea why the ## part doesn't seem to work?

    --Steve
    mrstephengross, Oct 11, 2005
    #4
  5. mrstephengross

    Ian Guest

    mrstephengross wrote:
    > #define OPER(op) operator ## op
    >
    > class thing
    > {
    > public:
    >
    > bool OPER(==) (const thing & other) const { return true; }
    > };
    >
    >
    > int main() { return 0; }
    > =============================
    >
    > The error reads: (line 7): pasting "operator" and "==" does not give a
    > valid preprocessing token
    >
    > Any ideas?
    >

    Looks like a compiler bug, when adding the expected result to the class,
    I get

    gcc /tmp/x.cc -E

    class thing
    {
    public:

    /tmp/x.cc:7:1: pasting "operator" and "==" does not give a valid
    preprocessing token
    bool operator== (const thing & other) const { return true; }
    bool operator== (const thing & other) const { return true; }
    };

    I think the use of a macro here is questionable, why bother? It just
    obscures the code.

    Ian
    Ian, Oct 11, 2005
    #5
  6. mrstephengross

    Maett Guest

    mrstephengross wrote:

    > Aha! Ok, so that works. That certainly solves my problem; do you have
    > any idea why the ## part doesn't seem to work?
    >
    > --Steve


    In the GCC preprocessor manual
    http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html#Concatenation
    they write:
    .... Two tokens that don't together form a valid token cannot be pasted
    together. For example, you cannot concatenate x with + in either order.
    If you try, the preprocessor issues a warning and emits the two tokens.
    Whether it puts white space between the tokens is undefined. It is
    common to find unnecessary uses of `##' in complex macros. If you get
    this warning, it is likely that you can simply remove the `##'...

    Maett
    Maett, Oct 11, 2005
    #6
  7. mrstephengross schrieb:
    > I'm using gcc 3.3.1 to compile the following code (below). I've written
    > a macro to simplify writing operators. The macro uses the '##' operator
    > to paste together 'operator' and the name of the operator (ie: '+').


    What do you mean by simplifying? Why it would be simplifier to write
    OPER(++) instead of operator++?

    The ## is for combining two tokens into one token, but "operator=="
    don't have to be one token, it is "operator" and "==". Combining them
    confuses the compiler.

    Thomas
    Thomas J. Gritzan, Oct 11, 2005
    #7
  8. mrstephengross

    Greg Guest

    mrstephengross wrote:
    > Aha! Ok, so that works. That certainly solves my problem; do you have
    > any idea why the ## part doesn't seem to work?


    The rule is that the concatenation produced by the ## operator must be
    a valid preprocessing token - even if the concatenation is not used as
    such.

    The result of the concatenation, operator== is not a valid preprocessor
    token because:

    #if operator==

    is not a legal preprocessor directive. A preprocessor token can
    comprise only of letters, digits and the underscore character.

    Greg
    Greg, Oct 11, 2005
    #8
  9. mrstephengross

    Jack Klein Guest

    On 11 Oct 2005 12:04:04 -0700, "Dan Cernat" <> wrote
    in comp.lang.c++:

    >
    > mrstephengross wrote:
    > > I'm using gcc 3.3.1 to compile the following code (below). I've written
    > > a macro to simplify writing operators. The macro uses the '##' operator
    > > to paste together 'operator' and the name of the operator (ie: '+').
    > > gcc reports that pasting the two things together "does not give a valid
    > > preprocessing token". Here's the code:
    > >
    > > ====================
    > > #define OPER(op) operator ## op
    > >
    > > class thing
    > > {
    > > public:
    > >
    > > bool OPER(==) (const thing & other) const { return true; }
    > > };
    > >
    > >
    > > int main() { return 0; }
    > > =============================
    > >
    > > The error reads: (line 7): pasting "operator" and "==" does not give a
    > > valid preprocessing token
    > >
    > > Any ideas?
    > >
    > > Thanks,
    > > --Steve ()

    >
    > Compiles fine with VC 6.0 and 7.1
    >
    > /dan


    All that proves is that the VC preprocessor has a defect, as far as
    the language standard is concerned. The result of the macro expansion
    is 'operator==', with no white space, and this is indeed not a valid
    preprocessing token. The gcc error message is absolutely correct.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Oct 12, 2005
    #9
    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. walala
    Replies:
    3
    Views:
    4,783
    walala
    Sep 18, 2003
  2. Paul Davis
    Replies:
    7
    Views:
    592
    Paul Davis
    Jul 17, 2003
  3. Cronus
    Replies:
    1
    Views:
    643
    Paul Mensonides
    Jul 15, 2004
  4. Preprocessor define concatenation

    , Jan 7, 2005, in forum: C Programming
    Replies:
    3
    Views:
    557
    Keith Thompson
    Jan 8, 2005
  5. glen herrmannsfeldt

    The or operator and the preprocessor?

    glen herrmannsfeldt, Feb 13, 2012, in forum: C++
    Replies:
    3
    Views:
    289
    Ian Collins
    Feb 13, 2012
Loading...

Share This Page