int 型から Boost.MPL の Sequence に変換する

mpl::int_<4241> → mpl::vector_c<int, 4, 2, 4, 1>

みたいな感じですね。

[ソース]

#include <boost/mpl/vector.hpp>
#include <boost/mpl/push_front.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/divides.hpp>
#include <boost/mpl/modulus.hpp>
#include <boost/mpl/less_equal.hpp>

namespace mpl = boost::mpl;

template<
    typename N,
    typename Seq
>
struct to_sequence_impl :
    mpl::if_<
        mpl::less_equal<N, mpl::int_<0> >,
        Seq,
        to_sequence_impl<
            mpl::divides<N, mpl::int_<10> >,
            typename mpl::push_front<
                Seq,
                typename mpl::modulus<N, mpl::int_<10> >::type
            >::type
        >
    >::type::type
{};

template<typename N>
struct to_sequence : to_sequence_impl<N, mpl::vector<> >{};


#include <boost/mpl/print.hpp>
#include <boost/mpl/equal.hpp>
#include <boost/mpl/vector_c.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/size_t.hpp>
#include <boost/mpl/equal_to.hpp>

BOOST_MPL_ASSERT(( mpl::equal<
    to_sequence<mpl::int_<341> >,
    mpl::vector_c<int, 3, 4, 1>,
    mpl::equal_to<mpl::_1, mpl::_2>
> ));

BOOST_MPL_ASSERT(( mpl::equal<
    to_sequence<mpl::size_t<1111> >,
    mpl::vector_c<int, 1, 1, 1, 1>,
    mpl::equal_to<mpl::_1, mpl::_2>
> ));

int
main(){
    
    return 0;
}

まぁ再帰して、処理しているだけですね。

[boost]

  • ver 1.47.0