has_member.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright (c) 2009-2020 Vladimir Batov.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. #ifndef BOOST_CONVERT_HAS_MEMBER_HPP
  5. #define BOOST_CONVERT_HAS_MEMBER_HPP
  6. #include <boost/type_traits/detail/yes_no_type.hpp>
  7. // This macro allows to check if a type has a member named "__member_name__"...
  8. // ... regardless of the signature. If takes advantage of the following behavior related to
  9. // function resolution. Say, both, foo and base, declare a method with the same name "func":
  10. //
  11. // struct foo { int func (int, int) { return 0; } };
  12. // struct base { void func () {} };
  13. // struct mixin : public foo, public base {};
  14. //
  15. // Now, if we inherit from both -- foo and base -- classes, then the following calls will fail
  16. // mixin_ptr(0)->func();
  17. // mixin_ptr(0)->func(5, 5);
  18. // with the error message (gcc): request for member func is ambiguous
  19. // regardless if we provide any arguments or not even though one might expect that
  20. // arg-based signature resolution might kick in. The only way to deploy those methods is:
  21. //
  22. // mixin_ptr(0)->foo::func();
  23. // mixin_ptr(0)->base::func(5, 5);
  24. //
  25. // C2. The actual signature of __member_name__ is not taken into account. If
  26. // __T__::__member_name__(any-signature) exists, then the introduced base::__member_name__
  27. // will cause mixin->__member_name__() call to fail to compile (due to ambiguity).
  28. // C3. &U::__member_name__ (a.k.a. &mixin::__member_name__)
  29. // has the type of func_type only if __T__::__member_name__ does not exist.
  30. // If __T__::member_name does exist, then mixin::__member_name__ is ambiguous
  31. // and "yes_type test (...)" kicks in instead.
  32. // C4. Need to find some unique/ugly name so that it does not clash if this macro is
  33. // used inside some other template class;
  34. #define BOOST_DECLARE_HAS_MEMBER(__trait_name__, __member_name__) \
  35. \
  36. template <typename __boost_has_member_T__> /*C4*/ \
  37. class __trait_name__ \
  38. { \
  39. using check_type = typename boost::remove_const<__boost_has_member_T__>::type; \
  40. using yes_type = ::boost::type_traits::yes_type; \
  41. using no_type = ::boost::type_traits:: no_type; \
  42. \
  43. struct base { void __member_name__(/*C2*/) {}}; \
  44. struct mixin : public base, public check_type {}; \
  45. \
  46. template <void (base::*)()> struct aux {}; \
  47. \
  48. template <typename U> static no_type test(aux<&U::__member_name__>*); /*C3*/ \
  49. template <typename U> static yes_type test(...); \
  50. \
  51. public: \
  52. \
  53. BOOST_STATIC_CONSTANT(bool, value = (sizeof(yes_type) == sizeof(test<mixin>(0)))); \
  54. }
  55. #endif // BOOST_CONVERT_HAS_MEMBER_HPP