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");
}
原文地址:https://www.cnblogs.com/prajna/p/2934410.html