[Home]CPPTM Answers - Exercise 4-4

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

Difference (from prior major revision) (no other diffs)

Added: 30a31,52

/////////////////////////////////////////////////////////////////
// Andriy Tylychko (telya at mail dot ru)
// a slightly simpler version:

template<typename T>
struct is_data_member_pointer
: mpl::and_<
boost::is_member_pointer<T>,
mpl::not_<boost::is_member_function_pointer<T> >
>
{};

// the same simplification can be applied to all items
// and the simplest possible version :)

template <typename T>
struct is_data_member_pointer: boost::is_member_object_pointer<T>
{};

//////////////////////////////////////////////////////////////////


 // (by Ariel Badichi)
 #include <boost/static_assert.hpp>
 #include <boost/type_traits/is_member_pointer.hpp>
 #include <boost/type_traits/is_member_function_pointer.hpp>
 #include <boost/type_traits/is_pointer.hpp>
 #include <boost/type_traits/is_function.hpp>
 #include <boost/type_traits/is_reference.hpp>
 #include <boost/type_traits/is_const.hpp>
 #include <boost/type_traits/remove_pointer.hpp>
 #include <boost/type_traits/remove_reference.hpp>
 #include <boost/mpl/and.hpp>
 #include <boost/mpl/not.hpp>
 #include <boost/mpl/eval_if.hpp>
 #include <boost/mpl/bool.hpp>

 namespace mpl = boost::mpl;

 class foo {};

 template<typename T>
 struct is_data_member_pointer
     : mpl::eval_if<
           mpl::and_<
               boost::is_member_pointer<T>,
               mpl::not_<boost::is_member_function_pointer<T> >
           >,
           mpl::true_,
           mpl::false_
       >
 {};

 /////////////////////////////////////////////////////////////////
 // Andriy Tylychko (telya at mail dot ru)
 // a slightly simpler version:

 template<typename T>
 struct is_data_member_pointer
     : mpl::and_<
           boost::is_member_pointer<T>,
           mpl::not_<boost::is_member_function_pointer<T> >
       >
 {};

 // the same simplification can be applied to all items
 // and the simplest possible version :)

 template <typename T>
 struct is_data_member_pointer: boost::is_member_object_pointer<T>
 {};

 //////////////////////////////////////////////////////////////////

 template<typename T>
 struct is_pointer_to_function
     : mpl::eval_if<
           mpl::and_<
               boost::is_pointer<T>,
               boost::is_function<typename boost::remove_pointer<T>::type>
           >,
           mpl::true_,
           mpl::false_
       >
 {};

 template<typename T>
 struct is_reference_to_function_pointer
     : mpl::eval_if<
           mpl::and_<
               boost::is_reference<T>,
               is_pointer_to_function<
                   typename boost::remove_reference<T>::type
               >
           >,
           mpl::true_,
           mpl::false_
       >
 {};

 template<typename T>
 struct is_reference_to_non_const
     : mpl::eval_if<
           mpl::and_<
               boost::is_reference<T>,
               mpl::not_<
                   boost::is_const<
                       typename boost::remove_reference<T>::type
                   >
               >
           >,
           mpl::true_,
           mpl::false_
       >
 {};

 int main()
 {
     typedef int (foo::*pmd);
     typedef int (foo::*pmf)();

     BOOST_STATIC_ASSERT((is_data_member_pointer<int>::type::value == false));
     BOOST_STATIC_ASSERT((is_data_member_pointer<pmd>::type::value == true));
     BOOST_STATIC_ASSERT((is_data_member_pointer<pmf>::type::value == false));

     typedef double (*pf)();

     BOOST_STATIC_ASSERT((is_pointer_to_function<int>::type::value == false));
     BOOST_STATIC_ASSERT((is_pointer_to_function<pf>::type::value == true));
     BOOST_STATIC_ASSERT((is_pointer_to_function<int *>::type::value == false));
     BOOST_STATIC_ASSERT((is_pointer_to_function<pmf>::type::value == false));

     typedef pf & rfp;

     BOOST_STATIC_ASSERT((is_reference_to_function_pointer<int>::type::value == false));
     BOOST_STATIC_ASSERT((is_reference_to_function_pointer<rfp>::type::value == true));
     BOOST_STATIC_ASSERT((is_reference_to_function_pointer<int &>::type::value == false));

     typedef const int & refc;

     BOOST_STATIC_ASSERT((is_reference_to_non_const<int>::type::value == false));
     BOOST_STATIC_ASSERT((is_reference_to_non_const<rfp>::type::value == true));
     BOOST_STATIC_ASSERT((is_reference_to_non_const<refc>::type::value == false));

     return 0;
 }

BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited July 22, 2006 6:22 am (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers