Skip to content
Snippets Groups Projects
Commit bf1edeca authored by Rainer Kartmann's avatar Rainer Kartmann
Browse files

Add macro to define has_member_xyz type trait.

parent e8c4dace
No related branches found
No related tags found
No related merge requests found
......@@ -53,6 +53,7 @@ SET(INCLUDES
EigenStdVector.h
meta/eigen/enable_if_compile_time_size.h
meta/has_member_macros/has_member.hpp
algorithm/for_each_if.h
......
// from: https://gist.github.com/maddouri/0da889b331d910f35e05ba3b7b9d869b
// A compile-time method for checking the existence of a class member
// @see https://general-purpose.io/2017/03/10/checking-the-existence-of-a-cpp-class-member-at-compile-time/
// This code uses "decltype" which, according to http://en.cppreference.com/w/cpp/compiler_support
// should be supported by Clang 2.9+, GCC 4.3+ and MSVC 2010+ (if you have an older compiler, please upgrade :)
// As of "constexpr", if not supported by your compiler, you could try "const"
// or use the value as an inner enum value e.g. enum { value = ... }
// check "test_has_member.cpp" for a usage example
/// Defines a "has_member_member_name" class template
///
/// This template can be used to check if its "T" argument
/// has a data or function member called "member_name"
#define define_has_member(member_name) \
template <typename T> \
class has_member_##member_name \
{ \
typedef char yes_type; \
typedef long no_type; \
template <typename U> static yes_type test(decltype(&U::member_name)); \
template <typename U> static no_type test(...); \
public: \
static constexpr bool value = sizeof(test<T>(0)) == sizeof(yes_type); \
}
/// Shorthand for testing if "class_" has a member called "member_name"
///
/// @note "define_has_member(member_name)" must be used
/// before calling "has_member(class_, member_name)"
#define has_member(class_, member_name) has_member_##member_name<class_>::value
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment