2 #ifndef BOOST_MPL_HAS_XXX_HPP_INCLUDED
3 #define BOOST_MPL_HAS_XXX_HPP_INCLUDED
5 // Copyright Aleksey Gurtovoy 2002-2006
6 // Copyright David Abrahams 2002-2003
8 // Distributed under the Boost Software License, Version 1.0.
9 // (See accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
12 // See http://www.boost.org/libs/mpl for documentation.
14 // $Id: has_xxx.hpp 49273 2008-10-11 06:54:06Z agurtovoy $
15 // $Date: 2008-10-11 02:54:06 -0400 (Sat, 11 Oct 2008) $
18 #include <boost/mpl/bool.hpp>
19 #include <boost/mpl/aux_/type_wrapper.hpp>
20 #include <boost/mpl/aux_/yes_no.hpp>
21 #include <boost/mpl/aux_/config/has_xxx.hpp>
22 #include <boost/mpl/aux_/config/msvc_typename.hpp>
23 #include <boost/mpl/aux_/config/msvc.hpp>
24 #include <boost/mpl/aux_/config/static_constant.hpp>
25 #include <boost/mpl/aux_/config/workaround.hpp>
27 #include <boost/preprocessor/cat.hpp>
29 #if BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x590) )
30 # include <boost/type_traits/is_class.hpp>
33 #if !defined(BOOST_MPL_CFG_NO_HAS_XXX)
35 # if BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
37 // agurt, 11/sep/02: MSVC-specific version (< 7.1), based on a USENET
38 // newsgroup's posting by John Madsen (comp.lang.c++.moderated,
39 // 1999-11-12 19:17:06 GMT); the code is _not_ standard-conforming, but
40 // it works way more reliably than the SFINAE-based implementation
42 // Modified dwa 8/Oct/02 to handle reference types.
44 # include <boost/mpl/if.hpp>
45 # include <boost/mpl/bool.hpp>
47 namespace boost { namespace mpl { namespace aux {
51 #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
52 template< typename U > struct msvc_incomplete_array
54 typedef char (&type)[sizeof(U) + 1];
58 template< typename T >
59 struct msvc_is_incomplete
61 // MSVC is capable of some kinds of SFINAE. If U is an incomplete
62 // type, it won't pick the second overload
63 static char tester(...);
65 #if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
66 template< typename U >
67 static typename msvc_incomplete_array<U>::type tester(type_wrapper<U>);
69 template< typename U >
70 static char (& tester(type_wrapper<U>) )[sizeof(U)+1];
73 BOOST_STATIC_CONSTANT(bool, value =
74 sizeof(tester(type_wrapper<T>())) == 1
79 struct msvc_is_incomplete<int>
81 BOOST_STATIC_CONSTANT(bool, value = false);
86 # define BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF_(trait, name, default_) \
87 template< typename T, typename name = ::boost::mpl::aux::has_xxx_tag > \
88 struct BOOST_PP_CAT(trait,_impl) : T \
90 static boost::mpl::aux::no_tag \
91 test(void(*)(::boost::mpl::aux::has_xxx_tag)); \
93 static boost::mpl::aux::yes_tag test(...); \
95 BOOST_STATIC_CONSTANT(bool, value = \
96 sizeof(test(static_cast<void(*)(name)>(0))) \
97 != sizeof(boost::mpl::aux::no_tag) \
99 typedef boost::mpl::bool_<value> type; \
102 template< typename T, typename fallback_ = boost::mpl::bool_<default_> > \
104 : boost::mpl::if_c< \
105 boost::mpl::aux::msvc_is_incomplete<T>::value \
106 , boost::mpl::bool_<false> \
107 , BOOST_PP_CAT(trait,_impl)<T> \
112 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, void) \
113 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, bool) \
114 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, char) \
115 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, signed char) \
116 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, unsigned char) \
117 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, signed short) \
118 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, unsigned short) \
119 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, signed int) \
120 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, unsigned int) \
121 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, signed long) \
122 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, unsigned long) \
123 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, float) \
124 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, double) \
125 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, long double) \
128 # define BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, T) \
129 template<> struct trait<T> \
131 BOOST_STATIC_CONSTANT(bool, value = false); \
132 typedef boost::mpl::bool_<false> type; \
136 #if !defined(BOOST_NO_INTRINSIC_WCHAR_T)
137 # define BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(trait, name, unused) \
138 BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF_(trait, name, unused) \
139 BOOST_MPL_AUX_HAS_XXX_TRAIT_SPEC(trait, wchar_t) \
142 # define BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(trait, name, unused) \
143 BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF_(trait, name, unused) \
148 // SFINAE-based implementations below are derived from a USENET newsgroup's
149 // posting by Rani Sharoni (comp.lang.c++.moderated, 2002-03-17 07:45:09 PST)
151 # elif BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1400)) \
152 || BOOST_WORKAROUND(__IBMCPP__, <= 700)
156 // agurt, 15/jun/05: replace overload-based SFINAE implementation with SFINAE
157 // applied to partial specialization to fix some apparently random failures
158 // (thanks to Daniel Wallin for researching this!)
160 # define BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(trait, name, default_) \
161 template< typename T > \
162 struct BOOST_PP_CAT(trait, _msvc_sfinae_helper) \
167 template< typename T, typename U = void > \
168 struct BOOST_PP_CAT(trait,_impl_) \
170 BOOST_STATIC_CONSTANT(bool, value = false); \
171 typedef boost::mpl::bool_<value> type; \
174 template< typename T > \
175 struct BOOST_PP_CAT(trait,_impl_)< \
177 , typename BOOST_PP_CAT(trait, _msvc_sfinae_helper)< typename T::name >::type \
180 BOOST_STATIC_CONSTANT(bool, value = true); \
181 typedef boost::mpl::bool_<value> type; \
184 template< typename T, typename fallback_ = boost::mpl::bool_<default_> > \
186 : BOOST_PP_CAT(trait,_impl_)<T> \
191 # elif BOOST_WORKAROUND( __BORLANDC__, BOOST_TESTED_AT(0x590) )
193 # define BOOST_MPL_HAS_XXX_TRAIT_NAMED_BCB_DEF(trait, trait_tester, name, default_) \
194 template< typename T, bool IS_CLASS > \
195 struct trait_tester \
197 BOOST_STATIC_CONSTANT( bool, value = false ); \
199 template< typename T > \
200 struct trait_tester< T, true > \
202 struct trait_tester_impl \
204 template < class U > \
205 static int resolve( boost::mpl::aux::type_wrapper<U> const volatile * \
206 , boost::mpl::aux::type_wrapper<typename U::name >* = 0 ); \
207 static char resolve( ... ); \
209 typedef boost::mpl::aux::type_wrapper<T> t_; \
210 BOOST_STATIC_CONSTANT( bool, value = ( sizeof( trait_tester_impl::resolve( static_cast< t_ * >(0) ) ) == sizeof(int) ) ); \
212 template< typename T, typename fallback_ = boost::mpl::bool_<default_> > \
215 BOOST_STATIC_CONSTANT( bool, value = (trait_tester< T, boost::is_class< T >::value >::value) ); \
216 typedef boost::mpl::bool_< trait< T, fallback_ >::value > type; \
219 # define BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(trait, name, default_) \
220 BOOST_MPL_HAS_XXX_TRAIT_NAMED_BCB_DEF( trait \
221 , BOOST_PP_CAT(trait,_tester) \
226 # else // other SFINAE-capable compilers
228 # define BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(trait, name, default_) \
229 template< typename T, typename fallback_ = boost::mpl::bool_<default_> > \
232 struct gcc_3_2_wknd \
234 template< typename U > \
235 static boost::mpl::aux::yes_tag test( \
236 boost::mpl::aux::type_wrapper<U> const volatile* \
237 , boost::mpl::aux::type_wrapper<BOOST_MSVC_TYPENAME U::name>* = 0 \
240 static boost::mpl::aux::no_tag test(...); \
243 typedef boost::mpl::aux::type_wrapper<T> t_; \
244 BOOST_STATIC_CONSTANT(bool, value = \
245 sizeof(gcc_3_2_wknd::test(static_cast<t_*>(0))) \
246 == sizeof(boost::mpl::aux::yes_tag) \
248 typedef boost::mpl::bool_<value> type; \
252 # endif // BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
255 #else // BOOST_MPL_CFG_NO_HAS_XXX
257 // placeholder implementation
259 # define BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(trait, name, default_) \
260 template< typename T, typename fallback_ = boost::mpl::bool_<default_> > \
263 BOOST_STATIC_CONSTANT(bool, value = fallback_::value); \
264 typedef fallback_ type; \
270 #define BOOST_MPL_HAS_XXX_TRAIT_DEF(name) \
271 BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(BOOST_PP_CAT(has_,name), name, false) \
274 #endif // BOOST_MPL_HAS_XXX_HPP_INCLUDED