Skip to content

Instantly share code, notes, and snippets.

@weslleyspereira
Created October 24, 2022 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weslleyspereira/0f5d344bd86bf57a1aa240b928f3a75e to your computer and use it in GitHub Desktop.
Save weslleyspereira/0f5d344bd86bf57a1aa240b928f3a75e to your computer and use it in GitHub Desktop.
Check if a class has a specific attribute
/** Defines structures that check if opts_t has an attribute called member.
*
* has_member<opts_t> is a true type if the struct opts_t has an attribute called member.
* has_member_v<opts_t> is true if the struct opts_t has a member called member.
*
* @param member Attribute to be checked
*
* @see https://stackoverflow.com/questions/1005476/how-to-detect-whether-there-is-a-specific-member-variable-in-class
*/
#define MEMBER_CHECKER(member) \
template< class opts_t, typename = int > \
struct has_ ## member : std::false_type { }; \
\
template< class opts_t > \
struct has_ ## member< opts_t, \
enable_if_t< \
!is_same_v< \
decltype(std::declval<opts_t>().member), \
void > \
, int > \
> : std::true_type { }; \
\
template< class opts_t > \
constexpr bool has_ ## member ## _v = has_ ## member<opts_t>::value;
// Activate checkers:
MEMBER_CHECKER(nb)
MEMBER_CHECKER(workPtr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment