is_dereferenceable.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2005-2007 Jonathan Turkanis
  3. // (C) Copyright David Abrahams 2004.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  6. // See http://www.boost.org/libs/iostreams for documentation.
  7. #ifndef BOOST_IOSTREAMS_DETAIL_IS_DEREFERENCEABLE_HPP_INCLUDED
  8. #define BOOST_IOSTREAMS_DETAIL_IS_DEREFERENCEABLE_HPP_INCLUDED
  9. # include <boost/type_traits/remove_cv.hpp>
  10. # include <boost/mpl/aux_/lambda_support.hpp>
  11. # include <boost/mpl/bool.hpp>
  12. # include <boost/detail/workaround.hpp>
  13. namespace boost { namespace iostreams { namespace detail {
  14. // is_dereferenceable<T> metafunction
  15. //
  16. // Requires: Given x of type T&, if the expression *x is well-formed
  17. // it must have complete type; otherwise, it must neither be ambiguous
  18. // nor violate access.
  19. // This namespace ensures that ADL doesn't mess things up.
  20. namespace is_dereferenceable_
  21. {
  22. // a type returned from operator* when no increment is found in the
  23. // type's own namespace
  24. struct tag {};
  25. // any soaks up implicit conversions and makes the following
  26. // operator* less-preferred than any other such operator that
  27. // might be found via ADL.
  28. struct any { template <class T> any(T const&); };
  29. // This is a last-resort operator* for when none other is found
  30. tag operator*(any const&);
  31. # if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3202))
  32. # define BOOST_comma(a,b) (a)
  33. # else
  34. // In case an operator++ is found that returns void, we'll use ++x,0
  35. tag operator,(tag,int);
  36. # define BOOST_comma(a,b) (a,b)
  37. # endif
  38. // two check overloads help us identify which operator++ was picked
  39. char (& check_increment(tag) )[2];
  40. template <class T>
  41. char check_increment(T const&);
  42. template <class T>
  43. struct impl
  44. {
  45. static typename boost::remove_cv<T>::type& x;
  46. BOOST_STATIC_CONSTANT(
  47. bool
  48. , value = sizeof(is_dereferenceable_::check_increment(BOOST_comma(*x,0))) == 1
  49. );
  50. };
  51. }
  52. # undef BOOST_comma
  53. template<typename T>
  54. struct is_dereferenceable
  55. : public ::boost::integral_constant<bool, is_dereferenceable_::impl<T>::value >
  56. {
  57. BOOST_MPL_AUX_LAMBDA_SUPPORT(1,is_dereferenceable,(T))
  58. };
  59. } }
  60. } // End namespaces detail, iostreams, boost.
  61. #endif // BOOST_IOSTREAMS_DETAIL_IS_DEREFERENCEABLE_HPP_INCLUDED