Created
October 24, 2022 21:05
-
-
Save weslleyspereira/0f5d344bd86bf57a1aa240b928f3a75e to your computer and use it in GitHub Desktop.
Check if a class has a specific attribute
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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