mpl::switch_

mpl で switch case が出来ないのか調べてみたらすでにあったみたいです。
リファレンスに載っていなかったので気付かなかった……。

#include <boost/static_assert.hpp>
#include <boost/type_traits.hpp>

#include <boost/mpl/assert.hpp>
#include <boost/mpl/switch.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/placeholders.hpp>

namespace mpl = boost::mpl;
using mpl::_;

typedef mpl::vector<
    mpl::pair<boost::is_pointer<_>,   boost::remove_pointer<_> >,
    mpl::pair<boost::is_reference<_>, boost::remove_reference<_> >,
    mpl::pair<boost::is_const<_>,     boost::remove_const<_> >
> switch_body;

typedef mpl::switch_<switch_body, int*>::type t1;
typedef mpl::switch_<switch_body, char&>::type t2;
typedef mpl::switch_<switch_body, float const>::type t3;

BOOST_MPL_ASSERT(( boost::is_same<t1, int> ));
BOOST_MPL_ASSERT(( boost::is_same<t2, char> ));
BOOST_MPL_ASSERT(( boost::is_same<t3, float> ));

int main(){}


また、複数の条件を満たす型は、上から順に適用されていくみたいです。

typedef mpl::switch_<switch_body, int const*>::type t4;
OOST_MPL_ASSERT(( boost::is_same<t4, int const> ));


以前、作成したものと使い方は殆ど同じですね……。
こっち使おう。


[boost]
ver 1.45.0