c++ meta function static_gcd

meta function,

boost mpl,

boost::common_factor库,

compile time:

 

template <unsigned int x, unsigned int y, bool is_ordered = (x >= y)>
struct static_gcd
{
    static int const value = static_gcd<y, x % y>::value;
};

template <unsigned int x, unsigned int y>
struct static_gcd<x, y, false>
{
    static int const value = static_gcd<y, x>::value;
};

template <unsigned int x>
struct static_gcd<x, 0, true>
{
    static int const value = x;
};

template <unsigned int x>
struct static_gcd<x, 1, true>
{
    static int const value = 1;
};

int main()
{
    std::cout << static_gcd<24, 32>::value << '\n';
    std::system("PAUSE");
}

 

posted on 2013-02-26 23:11  mhgu  阅读(264)  评论(0编辑  收藏  举报