|
||||
File indexing completed on 2025-01-18 09:30:27
0001 // Copyright (c) 2009-2020 Vladimir Batov. 0002 // Use, modification and distribution are subject to the Boost Software License, 0003 // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt. 0004 0005 #ifndef BOOST_CONVERT_HAS_MEMBER_HPP 0006 #define BOOST_CONVERT_HAS_MEMBER_HPP 0007 0008 #include <boost/type_traits/detail/yes_no_type.hpp> 0009 0010 // This macro allows to check if a type has a member named "__member_name__"... 0011 // ... regardless of the signature. If takes advantage of the following behavior related to 0012 // function resolution. Say, both, foo and base, declare a method with the same name "func": 0013 // 0014 // struct foo { int func (int, int) { return 0; } }; 0015 // struct base { void func () {} }; 0016 // struct mixin : public foo, public base {}; 0017 // 0018 // Now, if we inherit from both -- foo and base -- classes, then the following calls will fail 0019 // mixin_ptr(0)->func(); 0020 // mixin_ptr(0)->func(5, 5); 0021 // with the error message (gcc): request for member func is ambiguous 0022 // regardless if we provide any arguments or not even though one might expect that 0023 // arg-based signature resolution might kick in. The only way to deploy those methods is: 0024 // 0025 // mixin_ptr(0)->foo::func(); 0026 // mixin_ptr(0)->base::func(5, 5); 0027 // 0028 // C2. The actual signature of __member_name__ is not taken into account. If 0029 // __T__::__member_name__(any-signature) exists, then the introduced base::__member_name__ 0030 // will cause mixin->__member_name__() call to fail to compile (due to ambiguity). 0031 // C3. &U::__member_name__ (a.k.a. &mixin::__member_name__) 0032 // has the type of func_type only if __T__::__member_name__ does not exist. 0033 // If __T__::member_name does exist, then mixin::__member_name__ is ambiguous 0034 // and "yes_type test (...)" kicks in instead. 0035 // C4. Need to find some unique/ugly name so that it does not clash if this macro is 0036 // used inside some other template class; 0037 0038 #define BOOST_DECLARE_HAS_MEMBER(__trait_name__, __member_name__) \ 0039 \ 0040 template <typename __boost_has_member_T__> /*C4*/ \ 0041 class __trait_name__ \ 0042 { \ 0043 using check_type = typename boost::remove_const<__boost_has_member_T__>::type; \ 0044 using yes_type = ::boost::type_traits::yes_type; \ 0045 using no_type = ::boost::type_traits:: no_type; \ 0046 \ 0047 struct base { void __member_name__(/*C2*/) {}}; \ 0048 struct mixin : public base, public check_type {}; \ 0049 \ 0050 template <void (base::*)()> struct aux {}; \ 0051 \ 0052 template <typename U> static no_type test(aux<&U::__member_name__>*); /*C3*/ \ 0053 template <typename U> static yes_type test(...); \ 0054 \ 0055 public: \ 0056 \ 0057 BOOST_STATIC_CONSTANT(bool, value = (sizeof(yes_type) == sizeof(test<mixin>(0)))); \ 0058 } 0059 0060 #endif // BOOST_CONVERT_HAS_MEMBER_HPP
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |