mpl::switch_ に case と default が欲しい
つくりました。
これでだいぶ見やすくなったと思います。
#include <boost/mpl/switch.hpp> #include <boost/mpl/and.hpp> #include <boost/mpl/vector.hpp> #include <boost/mpl/string.hpp> #include <boost/mpl/range_c.hpp> #include <boost/mpl/always.hpp> #include <boost/mpl/modulus.hpp> #include <boost/utility/enable_if.hpp> #include "switch.hpp" namespace mpl = boost::mpl; using mpl::_1; typedef mpl::string<'fizz'> fizz; typedef mpl::string<'buzz'> buzz; typedef mpl::string<'fizz', 'buzz'> fizz_buzz; typedef mpl::equal_to<mpl::modulus<_1, mpl::int_<3> >, mpl::int_<0> > is_fizz; typedef mpl::equal_to<mpl::modulus<_1, mpl::int_<5> >, mpl::int_<0> > is_buzz; typedef mpl::and_<is_fizz, is_buzz> is_fizz_buzz; typedef mpl::vector< mpl::case_<is_fizz_buzz, mpl::c_str<fizz_buzz> >, mpl::case_<is_fizz, mpl::c_str<fizz> >, mpl::case_<is_buzz, mpl::c_str<buzz> >, mpl::default_< _1> > case_fizz_buzz; #include <iostream> #include <boost/mpl/for_each.hpp> struct print{ template<typename T> void operator()(T){ typedef typename mpl::switch_<case_fizz_buzz, T>::type result; std::cout << result::value << ","; } }; int main(){ mpl::for_each< mpl::range_c<int, 1, 20> >( print() ); return 0; }
[出力]
1,2,fizz,4,buzz,fizz,7,8,fizz,buzz,11,fizz,13,14,fizz_buzz,16,17,fizz,19,
mpl::case_ と mpl::default を追加。
これで満足。
[boost]
ver 1.46.1
switch.hpp の中身は以下から。
[switch.hpp]
#ifndef __SWITCH__H_ #define __SWITCH__H_ #include <boost/mpl/switch.hpp> namespace boost { namespace mpl { template<typename F, typename S, typename T = void> struct case_; template<typename F, typename S> struct case_<F, S, typename enable_if<is_lambda_expression<S> >::type> : pair<F, S> {}; template<typename F, typename S> struct case_<F, S, typename disable_if<is_lambda_expression<S> >::type> : pair<F, always<S> > {}; template<typename S> struct default_ : pair<always<mpl::true_>, S>{}; } } // namespace boost { namespace mpl { #endif /* _SWITCH_ */