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
c ++ macros include-guards


source to share


No one has answered this question yet

See similar questions:

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

or similar:

684
Why use the obviously meaningless do-while and if-else statements in macros?
286
Strange definitions of the TRUE and FALSE macros
240
What makes Lisp macros so special?
13
Is_constexpr_copiable implementation
12
Template function dependency on identical signatures, why does it work?
4
Failed to customize function template
3
The default parameter in the template & # 8594; the template argument includes a template parameter
2
why type_identity breaks the is_detected implementation
1
How to specialize class template methods based on type traits? Using std :: enable_if works for non-classical functions, but fails for class methods
0
Overlay security type on existing C-intrusive linked lists



All Articles
Loading...
X
Show
Funny
Dev
Pics