Boost.MPL の divides を実行時に計算
#include <boost/mpl/divides.hpp> #include <boost/mpl/int.hpp> #include <iostream> namespace mpl = boost::mpl; template<typename T1, typename T2> struct divides : mpl::divides<T1, T2>{ template<typename T> operator T() const{ return static_cast<T>(T1::value) / static_cast<T>(T2::value); } }; typedef divides<mpl::int_<355>, mpl::int_<113> > PI; int main(){ float n = divides<mpl::int_<5>, mpl::int_<2> >(); std::cout << n << std::endl; double p = PI(); std::cout << p << std::endl; return 0; }
[出力]
2.5 3.14159
mpl::divides は、コンパイル時に計算を行うので、
mpl::divides<mpl::int_<5>, mpl::int_<2> >() == 2;
になりますが、上記の場合は、実行時に計算を行っているので擬似的に実数を保持することが出来ます。
そして、今気づきましたけど、これって Boost.Ratio でいいんじゃね?
あとで試してみよう……。
可変長?なにそれおいしいの。
[boost]
ver 1.46.1
[参照]
http://www.boost.org/doc/libs/1_46_1/libs/mpl/doc/refmanual/integral-constant.html