Problem with macro expansion

Discussion in 'C++' started by me, Nov 9, 2004.

  1. me

    me Guest

    Hi guys

    I want to insert a load of pieces of data into a map

    The map has an std::string representing a field name as the key, and the
    value is a struct with 2 members - the field length and a bool
    indicating whether the field is a special field or not.

    The struct representing the value has a constructor that takes a single
    parameter for the length, and defaults the boolean to false.

    To simplify the code, and make things look neater, I defined a macro:

    #define SETUP_FIELD(FIELD_NAME, FIELD_SIZE)
    FieldMap.insert(std::make_pair(std::string("FIELD_NAME"),FieldData(FIELD_SIZE)))
    ;

    So i can do:

    SETUP_FIELD (TheFirstField,3) ;
    SETUP_FIELD (TheSecondField,1) ;
    SETUP_FIELD (TheThirdField,2) ;
    SETUP_FIELD (TheFourthField,7) ;

    etc...

    But when i look at the map, all the keys are set to "FIELD_NAME", rather
    than "TheFirstField" etc - the macro has not substituted the string I
    pass in. I guess this is something to do with substitution within a
    string literal...but I'm a bit stumped.

    Any Ideas?

    dtw
     
    me, Nov 9, 2004
    #1
    1. Advertising

  2. me wrote:
    > I want to insert a load of pieces of data into a map
    >
    > The map has an std::string representing a field name as the key, and the
    > value is a struct with 2 members - the field length and a bool
    > indicating whether the field is a special field or not.
    >
    > The struct representing the value has a constructor that takes a single
    > parameter for the length, and defaults the boolean to false.
    >
    > To simplify the code, and make things look neater, I defined a macro:
    >
    > #define SETUP_FIELD(FIELD_NAME, FIELD_SIZE)
    > FieldMap.insert(std::make_pair(std::string("FIELD_NAME"),FieldData(FIELD_SIZE)))


    FieldMap.insert(std::make_pair(std::string(#FIELD_NAME), \
    FieldData(FIELD_SIZE)));

    RTFM on the '#' stringizing operator.

    > ;
    >
    > So i can do:
    >
    > SETUP_FIELD (TheFirstField,3) ;
    > SETUP_FIELD (TheSecondField,1) ;
    > SETUP_FIELD (TheThirdField,2) ;
    > SETUP_FIELD (TheFourthField,7) ;
    >
    > etc...
    >
    > But when i look at the map, all the keys are set to "FIELD_NAME", rather
    > than "TheFirstField" etc - the macro has not substituted the string I
    > pass in. I guess this is something to do with substitution within a
    > string literal...


    Yes, there is no such substitution.

    >but I'm a bit stumped.
    >
    > Any Ideas?


    See above.

    V
     
    Victor Bazarov, Nov 9, 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. Benjamin Niemann
    Replies:
    3
    Views:
    330
    Caleb Hattingh
    Aug 26, 2004
  2. Ark

    A question on macro expansion

    Ark, Jul 22, 2004, in forum: C Programming
    Replies:
    3
    Views:
    355
  3. Vittal

    Macro Expansion

    Vittal, Mar 22, 2005, in forum: C Programming
    Replies:
    3
    Views:
    412
    Eric Sosman
    Mar 23, 2005
  4. Dom Gilligan

    Macro expansion of '#__LINE__'?

    Dom Gilligan, Nov 4, 2005, in forum: C Programming
    Replies:
    4
    Views:
    455
    Dom Gilligan
    Nov 4, 2005
  5. reppisch

    partial/selective Macro expansion scpp

    reppisch, May 5, 2006, in forum: C Programming
    Replies:
    7
    Views:
    370
    michi
    May 7, 2006
Loading...

Share This Page