Thread: Paste '::' in using a macro

  1. #1
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528

    Paste '::' in using a macro

    Is there a way I could get around gcc and clang and paste '::' from am macro?

    Code:
    #define reg(f, t, p) \
         object::##f 
        /* do some stuff */
    I think MSVC will accept such code but gcc and clang do not.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe if object is a namespace name you could get away with:
    Code:
    #define reg(f, t, p) do { \
        using namespace object; \
        /* do some stuff */ \
    } while (false)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by laserlight View Post
    Maybe if object is a namespace name you could get away with:
    Code:
    #define reg(f, t, p) do { \
        using namespace object; \
        /* do some stuff */ \
    } while (false)
    Yeah that could work with an namespace.

    Am trying to assign member function of 'object' to another object.ahh
    Code:
    #define reg(f, t, p) do { \ 
         array[5] = object::##f \
         /* do some stuff */
    } while (false)
    f is a member of object.
    I have to assign about 100 of this functions so if I got around the macro, I could save myself a ton of code.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The C++ preprocessor is already aware that some_namespace::some_member is three distinct tokens some_namespace, ::, and some_member, so does not need to do token pasting. The solution is to remove the ##.

    Code:
    #define reg(f, t, p) do { \
         array[5] = object::f \
         /* do some stuff */
    } while (false)
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by grumpy View Post
    The C++ preprocessor is already aware that some_namespace::some_member is three distinct tokens some_namespace, ::, and some_member, so does not need to do token pasting. The solution is to remove the ##.

    Code:
    #define reg(f, t, p) do { \
         array[5] = object::f \
         /* do some stuff */
    } while (false)
    Aah, cool. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin + copy paste
    By Ducky in forum C++ Programming
    Replies: 5
    Last Post: 12-17-2012, 07:33 AM
  2. c++ paste from clipboard
    By hawlpm3 in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2011, 03:50 PM
  3. copy paste from another app
    By bonkey in forum Windows Programming
    Replies: 1
    Last Post: 09-08-2003, 08:03 AM
  4. Just paste this into your editor..
    By Linette in forum C++ Programming
    Replies: 21
    Last Post: 03-26-2002, 07:20 PM