preprocessor, token concatenation, no valid preprocessor token

Discussion in 'C++' started by Cronus, Jul 14, 2004.

  1. Cronus

    Cronus Guest

    Hi

    the following code the g++ (g++ (GCC) 3.3.3 (Debian 20040422)) emits the
    error message that pasting of :: and hello is no valid preprocessor token.
    The g++ 2.95.3 accepts the code. I know that handling of the ##
    preprocessing operator has changed.

    How I have to change the macro definiton of A(P)?
    NO changes at macro calling should be necessary!

    Thanks in advance!



    #define A(P)    int P##hello(void) {return 1;}

    class B {
    public:
            B() {
                    b = 1;
            }

            private:
                            int b;
                            int hello();
    };

    A(D40);
    A(B::);

    int main(void)  {
    }
    Cronus, Jul 14, 2004
    #1
    1. Advertising

  2. Cronus wrote:
    > Hi
    >
    > the following code the g++ (g++ (GCC) 3.3.3 (Debian 20040422)) emits
    > the error message that pasting of :: and hello is no valid
    > preprocessor token. The g++ 2.95.3 accepts the code. I know that
    > handling of the ## preprocessing operator has changed.
    >
    > How I have to change the macro definiton of A(P)?
    > NO changes at macro calling should be necessary!


    In general, it is not possible to do this. Pasting '::' to 'hello' is undefined
    behavior, and you can't make an identifier out of two other identifiers without
    token-pasting. Instead, you should get rid of the concatenation and pass the
    entire name:

    #define A(id) int id(void) {return 1;}

    A(D40hello)
    A(B::hello)

    Further, it is extremely likely that there is a better way to do whatever it is
    you're doing (such as tag dispatch + ADL).

    Regards,
    Paul Mensonides
    Paul Mensonides, Jul 15, 2004
    #2
    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,772
    walala
    Sep 18, 2003
  2. Preprocessor define concatenation

    , Jan 7, 2005, in forum: C Programming
    Replies:
    3
    Views:
    551
    Keith Thompson
    Jan 8, 2005
  3. G Fernandes
    Replies:
    1
    Views:
    503
  4. mrstephengross
    Replies:
    8
    Views:
    642
    Jack Klein
    Oct 12, 2005
  5. thomas

    token concatenation macro

    thomas, Nov 2, 2009, in forum: C++
    Replies:
    4
    Views:
    943
    James Kanze
    Nov 3, 2009
Loading...

Share This Page