簡體   English   中英

如何在模板元編程中進行比較?

[英]How to make a less than comparison in template meta-programming?

星期一我問過這個問題而且我的生活中我不知道該怎么回答。 由於我不知道,我現在想要非常了解。 好奇心正在殺死這只貓。 給定兩個整數,在編譯時返回較小的整數。

template<int M, int N>
struct SmallerOfMandN{
    //and magic happenes here
};

有指針或怎么做? (今晚將開始閱讀Boost MPL 。)

這被稱為兩個數字的最小值,你不需要像mpl這樣的世界重量級庫來做這樣的事情:

template <int M, int N>
struct compile_time_min
{
    static const int smaller =  M < N ? M : N;
};

int main()
{
    const int smaller = compile_time_min<10, 5>::smaller;
}

當然如果它是C ++ 0x你可以很容易地說:

constexpr int compile_time_min(int M, int N)
{
    return M < N ? M : N;
}

int main()
{
    constexpr int smaller = compile_time_min(10, 5);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM