0

Is there any way to use a prior defined macro in the concatenation using the ## operator?

I would like:

#define PREFIX MR_
#define MAKE_TITLE(NAME) PREFIX ## NAME
MAKE_TITLE(SMITH)

MR_SMITH

Rather the result is:

PREFIXSMITH

I do not want to include the prefix as an argument in the MAKE_TITLE macro as this macro may be used many times for various categories (MR, MS, etc.). Consider:

#define COUPLES\
    X(SMITH, 31, 35), \
    X(JONES, 41, 39), \
    X(BAKER, 51, 21)

#define X(SURNAME, AGE_HUSBAND, AGE_WIFE) PREFIX ## SURNAME = AGE_HUSBAND,
#define PREFIX MR_
enum HUSBAND_AGES
{
    COUPLES
};

#define X(SURNAME, AGE_HUSBAND, AGE_WIFE) PREFIX ## SURNAME = AGE_WIFE,
#define PREFIX MS_
enum WIFE_AGES
{
    COUPLES
}
Kay
  • 123
  • 8
  • You need a helper macro to expand the other macro before passing it on to the macro containing ## (or #). Because ## or # is always applied before macro expansion in the macro where they are found. – Lundin Aug 11 '23 at 08:20
  • Btw unrelated to the actual question, consider writing X macros in a manner where the macro is passed to the list, and also always write the list without commas between the items, for maximum flexibility. Check out this https://godbolt.org/z/7o6v4aEKb – Lundin Aug 11 '23 at 08:30

0 Answers0