Preprocessor Macro Problem

Discussion in 'Mac Programming' started by Richard Birkett, Feb 26, 2012.

  1. macrumors member

    Joined:
    Aug 21, 2011
    #1
    I have a macro like this:

    Code:
    #define IntMethods(lower, cap, parameter) \
    - (MMExpressionType)symboliccapWithInt:(SInt64)parameter\
    {\
    	return [(MMExpressionType)self symboliccap:[[MMTerm alloc] initWithInteger:parameter]];\
    }\
    \
    - (MMExpressionType)lowerWithInt:(SInt64)parameter\
    {\
    	return [(MMExpressionType)self lower:[[MMTerm alloc] initWithInteger:parameter]];\
    }
    It is supposed to replace a repetitive set of implementations, I hope you understand what I am trying to do. My code here gets errors, it seems to think I'm literally using symboliccap etc. instead of something using the macro parameter.
    Here is some desired output to input that will hopefully help if you don't understand.

    Code:
    IntMethods(add, Add, addend)
    should be equivalent to
    Code:
    - (MMExpressionType)symbolicAddWithInt:(SInt64)exponent
    {
    	return [(MMExpressionType)self symbolicAdd:[[MMTerm alloc] initWithInteger:addend]];
    }
    
    - (MMExpressionType)addWithInt:(SInt64)exponent
    {
    	return [(MMExpressionType)self add:[[MMTerm alloc] initWithInteger:addend]];
    }
    Help greatly appreciated. :)
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    http://gcc.gnu.org/onlinedocs/cpp/Concatenation.html

    Google search terms: c preprocessor token pasting

    This is by far the most complex feature of the preprocessor, and it has some quirks. So I strongly recommend reading more than one reference, and looking at more than one example.
     
  3. thread starter macrumors member

    Joined:
    Aug 21, 2011
    #3
    Thank you so much, that's perfect!
    Here's my solution for reference:
    Code:
    #define IntMethods(lower, cap, parameter) \
    - (MMExpressionType)symbolic##cap##WithInt:(SInt64)parameter\
    {\
    	return [(MMExpressionType)self symbolic##cap:[[MMTerm alloc] initWithInteger:parameter]];\
    }\
    \
    - (MMExpressionType)lower##WithInt:(SInt64)parameter\
    {\
    	return [(MMExpressionType)self lower:[[MMTerm alloc] initWithInteger:parameter]];\
    }
     
  4. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    And when you get your macros working, you might consider whether you are not just trying to be too clever for your own good. Nothing more pleasant than looking for the definition of a method and not being able to find it anywhere in your source code. Or not being able to set a breakpoint in some method because of these macros.
     
  5. thread starter macrumors member

    Joined:
    Aug 21, 2011
    #5
    Well as you can see it is simple code really and they are all in the header anyway.
     

Share This Page