2013-11-28 33 views
4

假設我有一些模板代碼執行以下操作:如何判斷具有帶符號數字類型的東西?

T x = foo(); 
T y = -x; 

現在,如果T是一個非數字型(或不具有執行一元減),編譯會因此失敗。但如果它是一個unsigned int,unsigned short等,它將成功,並帶有警告。所以我想能夠做到

T x = foo(); 
if (/* magic condition */ { 
    T y = -x; 
} 

我可以寫表達的條件 - 這是在編譯時或運行時檢查 - T的類型是一些符號數值類型?例如使用typeid?

注:

  • 斷言也將是不錯,但我喜歡的東西更加靈活。

回答

6

C++ 11有is_unsigned特質,你可以在一個static_assert使用:

#include <type_traits> 

template <typename T> 
void foo() 
{ 
    static_assert(std::is_unsigned<T>::value); 

    T x = /* ... */ 
    T y = -x; 

    /* ... */ 
} 

如果需要檢查,更有活力,那麼就堅持在一個if條件:

template <typename T> 
void foo() 
{ 
    if (!std::is_unsigned<T>::value) { 
     /* To arrive here and for the following 
      not to error out, we must have a numeric 
      type that's not unsigned! */ 

     T x = /* ... */ 
     T y = -x; 
    } 
    else { 
     /* Do something else for unsigned numeric 
      types */ 
    } 
} 

更復雜的解決方案涉及重載,std::enable_if和各種其他模板metahackery,但上述可能是所有你需要的。

+0

您能否更新您的答案以解決我的問題? – einpoklum

+0

@einpoklum:完成 –

+0

@einpoklum Huh,誰會想到當你需要動態分支時,你可以使用if :)。 – ScarletAmaranth

4

是的,你可以。

static_assert(std::is_unsigned<T>::value, "Not unsigned!"); 

(您需要包括type_traits這個工作。)

然後可以自然地調整你的編譯過程中,甚至有可能與enable_if如果你確信沒有其他辦法:)。

+0

我想要一個'if(...)',而不是一個斷言。將澄清我的問題。 – einpoklum

+0

@einpoklum然後只需使用標籤調度。雖然我不確定你會在'else'情況下做什麼(好吧,這個警告首先是愚蠢的,因爲無符號減號是完全明確的)。 –

+0

@ChristianRau:你的意思是,如[在這裏](http://www.generic-programming.org/languages/cpp/techniques.php#tag_dispatching)? – einpoklum

相關問題