Macro question

This is a discussion on Macro question within the C++ Programming forums, part of the General Programming Boards category; Code: #define XXX_A0_YYY "foo" #define XXX_A1_YYY "bar" #define getXXX(id) XXX_##id##_YYY The idea is that getXXX(A0) expands to XXX_A0_YYY and thus ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Macro question

    Code:

    #define XXX_A0_YYY "foo"
    #define XXX_A1_YYY "bar"
    #define getXXX(id) XXX_##id##_YYY

    The idea is that getXXX(A0) expands to XXX_A0_YYY and thus the result of "cout << getXXX(0)" is "foo".

    However, instead I get the following error message from the compiler: "XXX_id_YYY is undefined".

    Why is it not replacing 'id' with the passed parameter?

    If I remove "XXX_" from all of the above, it works correctly:

    #define A0_YYY "foo"
    #define A1_YYY "bar"
    #define getXXX(id) id##_YYY

    Now "cout << getXXX(A0)" works and prints "foo" on the screen.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,796
    Quote Originally Posted by Mole42
    The idea is that getXXX(A0) expands to XXX_A0_YYY and thus the result of "cout << getXXX(0)" is "foo".
    I think you meant to say that 'the result of "cout << getXXX(A0)" is "foo"'. This works for me:
    Code:
    #include <iostream>
    
    #define XXX_A0_YYY "foo"
    #define XXX_A1_YYY "bar"
    #define getXXX(id) XXX_##id##_YYY
    
    int main()
    {
        using namespace std;
        cout << getXXX(A0) << endl;
    }
    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
    Registered User
    Join Date
    Jul 2008
    Posts
    26
    I tried that, I get

    "Undefined identifier: XXX_id_YYY"

    If I change:

    #define getXXX(id) XXX_##id##_YYY

    to:

    #define getXXX(id) id##_YYY

    then I get

    "Undefined identifier: A0_YYY"

    Which is what I expect.

    I'm beginning to think it's a bug in the pre-processor.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,313
    You could be right there. What compiler is it?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I would expect that error message if you called it as getXXX(id) instead of getXXX(A0).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question, C #
    By mate222 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2009, 05:24 AM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. design question: opinion
    By ggs in forum C Programming
    Replies: 2
    Last Post: 01-29-2003, 10:59 AM
  5. macro (#define) question
    By Darrok in forum C++ Programming
    Replies: 30
    Last Post: 12-20-2001, 04:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21