Friday, September 2, 2016

Exercise 3-3 Abrahams & Gurtovoy

Turn T into T**** by using twice twice.

#include <boost/type_traits/add_pointer.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/static_assert.hpp>
template <class UnaryMetaFunctionClass, class Arg>
struct apply1
: UnaryMetaFunctionClass::template apply<Arg>
{};
template <class F, class X>
struct twice
: apply1<F, typename apply1<F,X>::type>
{};
struct add_pointer_f
{
template <class T>
struct apply : boost::add_pointer<T> {};
};
BOOST_STATIC_ASSERT((
boost::is_same<
twice<add_pointer_f,
twice<add_pointer_f, int>::type>::type
, int****
>::value
));
int main()
{
return 0;
}
view raw 3d3.cpp hosted with ❤ by GitHub

Exercise 3-1 of Abrahams & Gurtovoy

Turn vector_c<int,1,2,3> into a type sequence with elements (2,3,4) using transform.

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/static_assert.hpp>
typedef boost::mpl::vector_c<int,1,2,3> v_a;
typedef boost::mpl::vector_c<int,1,1,1> v_b;
typedef boost::mpl::vector_c<int,2,3,4> v_c0;
struct plus_f
{
template <class T1, class T2>
struct apply
{
typedef typename boost::mpl::plus<T1,T2>::type type;
};
};
typedef typename boost::mpl::transform<v_a,v_b,plus_f>::type v_c1;
BOOST_STATIC_ASSERT((
boost::mpl::equal<v_c0,v_c1>::type::value
));
int main()
{
return 0;
}
view raw 3d1.cpp hosted with ❤ by GitHub