Avoid deploying multiple macros to a single input

I want my macro to only expand once for each possible argument.

    #define PAX_CREATE_MEMBER_CHECKER_FOR_NAME(memberName) \
            template <typename T, typename = int> \
            struct HasMember_##memberName : std::false_type { }; \
            template <typename T> \
            struct HasMember_##memberName <T, decltype((void) T::memberName, 0)> : std::true_type { };

      

When I use the same thing memberName

twice, I don't want the macro to expand a second time, because it will lead to errors as functions are now defined twice. I tried to use macros inside macros, but it is clear to me that this will not work, since macros are expanded only once (sanely). Although this may clarify the question:

    #define PAX_CREATE_MEMBER_CHECKER_FOR_NAME(memberName) \
        #ifndef PAX_CREATE_MEMBER_CHECKER_FOR_NAME_##memberName \
        #define PAX_CREATE_MEMBER_CHECKER_FOR_NAME_##memberName \
            template <typename T, typename = int> \
            struct HasMember_##memberName : std::false_type { }; \
            template <typename T> \
            struct HasMember_##memberName <T, decltype((void) T::memberName, 0)> : std::true_type { }; \
        #endif

      

The template-based solution is great too!


This macro is a slightly modified version of the macro DEFINE_MEMBER_CHECKER

in

How can I tell if a particular member variable exists in a class?

+3


source to share





All Articles