読者です 読者をやめる 読者になる 読者になる

Boost.MPLのコンパイル時アルゴリズム

C++

Boost.MPLの練習。


find

#include <boost/mpl/vector.hpp>
#include <boost/mpl/find.hpp>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

using namespace boost;
using namespace boost::mpl;

int main()
{
    typedef vector<int, char, double> seq;
    typedef find<seq, char>::type     result;

    BOOST_STATIC_ASSERT((is_same<deref<result>::type, char>::value));
}


find_if

#include <boost/mpl/vector.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

using namespace boost;
using namespace boost::mpl;

int main()
{
    typedef vector<int, char, double> seq;
    typedef find_if<seq, is_same<_1, char> >::type result;

    BOOST_STATIC_ASSERT((is_same<deref<result>::type, char>::value));
}


count

#include <boost/mpl/vector.hpp>
#include <boost/mpl/count.hpp>
#include <boost/static_assert.hpp>

using namespace boost::mpl;

int main()
{
    typedef vector<int, char, int, double, int> seq;

    BOOST_STATIC_ASSERT((count<seq, int>::type::value == 3));
}


count_if

#include <boost/mpl/vector.hpp>
#include <boost/mpl/count_if.hpp>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

using namespace boost;
using namespace boost::mpl;

int main()
{
    typedef vector<int, char, int, double, int> seq;

    BOOST_STATIC_ASSERT((count_if<seq, is_same<_1, int> >::type::value == 3));
}


copy

#include <boost/mpl/vector.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

using namespace boost;
using namespace boost::mpl;

int main()
{
    typedef vector<int, char> seq;
    typedef copy<vector<double, long>, back_inserter<seq> >::type result;

    BOOST_STATIC_ASSERT((is_same<result, vector<int, char, double, long>::type>::value));
}


transform

#include <boost/mpl/vector.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

using namespace boost;
using namespace boost::mpl;

int main()
{
    typedef vector<int, char, double> seq;
    typedef transform<seq, add_pointer<_1> >::type result;

    BOOST_STATIC_ASSERT((is_same<result, vector<int*, char*, double*>::type>::value));
}


sort

#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/sort.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/static_assert.hpp>

using namespace boost::mpl;

int main()
{
    typedef vector_c<int, 3, 1, 4> seq1;
    typedef vector_c<int, 1, 3, 4> seq2;

    typedef sort<seq1>::type result;

    BOOST_STATIC_ASSERT((equal<result, seq2, equal_to<_, _> >::value));
}


unique

#include <boost/mpl/vector.hpp>
#include <boost/mpl/unique.hpp>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

using namespace boost;
using namespace boost::mpl;

int main()
{
    typedef vector<int, int, char, double> seq;
    typedef unique<seq, is_same<_1, _2> >::type result;

    BOOST_STATIC_ASSERT((is_same<result, vector<int, char, double>::type>::value));
}


reverse

#include <boost/mpl/vector.hpp>
#include <boost/mpl/reverse.hpp>
#include <boost/type_traits.hpp>
#include <boost/static_assert.hpp>

using namespace boost;
using namespace boost::mpl;

int main()
{
    typedef vector<int, char, double> seq;
    typedef reverse<seq>::type result;

    BOOST_STATIC_ASSERT((is_same<result, vector<double, char, int>::type>::value));
}