簡體   English   中英

C ++ 11類型檢查代碼的奇怪編譯器警告

[英]strange compiler warnings of C++11 Type checking code

我正在編寫帶有進位/溢出檢查的通用加法器,並且大量使用了c ++ 11類型檢查功能。

這是我的代碼:

#include <iostream>
using namespace std;
#define MIN_OF(TYPE) ( (std::is_signed<decltype(res)>::value) ? \
                                (1 << ( ( sizeof(decltype(res)) * 8 ) - 1)) : \
                                0 )

#define MAX_OF(TYPE) (~MIN_OF(TYPE))


#define ABS(x)  (x < 0 ? -x : x)

class Flags
{
public:
    void setSign(bool x)
    {
        cout << boolalpha;
        cout << "setSign: " << x << endl;
    }
    void setOverflow(bool x)
    {
        cout << boolalpha;
        cout << "setOverflow: " << x << endl;
    }
    void setCarry(bool x)
    {
        cout << boolalpha;
        cout << "setCarry: " << x << endl;
    }
    void setZero(bool x)
    {
        cout << boolalpha;
        cout << "setZero: " << x << endl;
    }
};

template <typename TYPE, TYPE def>
class Value
{
public:
static inline TYPE get()
{
    return def;
}
static inline void set(TYPE x)
{
    cout << "value: " << hex << x << endl;
}
};


template <class A, class B, class RES>
struct ADD
{
static void Do(Flags* _flags)
{
    if (std::is_convertible<decltype(A::get()),decltype(RES::get())>::value)
    {
        decltype(A::get()) _a = A::get();
        decltype(B::get()) _b = B::get();

        decltype(RES::get()) res = _a;

        if (_b != 0)
        {
            res = res + _b;

            if (std::is_signed<decltype(res)>::value)
            {
                unsigned char highestbit_a = static_cast<unsigned char>(0x1 & (_a >> (( sizeof(decltype(_a)) * 8 ) - 1)));
                unsigned char highestbit_b = static_cast<unsigned char>(0x1 & (_b >> (( sizeof(decltype(_b)) * 8 ) - 1)));
                unsigned char highestbit_res = static_cast<unsigned char>(0x1 & (res >> (( sizeof(decltype(res)) * 8 ) - 1)));

                _flags->setSign( (res < 0) );
                _flags->setOverflow( ((highestbit_a & highestbit_b) != highestbit_res) );
            }
            else
            {
                _flags->setSign( false );
                _flags->setOverflow( false );
            }

            bool setCarryFlag = false;

            if (std::is_signed<decltype(_b)>::value)
            {
                if(_b < 0)
                {
                    /* as _b is negative, we add _b to lowest_res, if the result
                     *  is greater as _a, _a + _b (with _b as negative number) would
                     * result in an carry out
                     */
                    setCarryFlag = (static_cast<decltype(_a)>(ABS((MIN_OF(decltype(res)) - _b))) > _a);
                }
                else
                {
                    setCarryFlag = (static_cast<decltype(_a)>((MAX_OF(decltype(res)) - _b)) < _a);
                }
            }
            else
            {
                //get difference of one summand to results highest until carry
                /* MARKED LINE: this branch gets wrongly checked */
                setCarryFlag = ((MAX_OF(decltype(res)) - _b) < _a);
            }

            _flags->setCarry( setCarryFlag );
        }
        else
        {
            if (std::is_signed<decltype(res)>::value)
            {
                _flags->setSign( (res < 0) );
            }
        }

        _flags->setZero( (res == 0) );

        //store result
        RES::set(res);
    }
}
};



int main()
{
Flags* f = new Flags();
ADD<Value<unsigned int, 1>, Value<signed int, 6>, Value<unsigned int, 1>>::Do(f);

return 0;
}

該問題發生在“ MARKED LINE:”。 通常,我會理解編譯器不會使用此分支,因為_b是有符號int的類型,因此is_signed應該為true,因此編譯器應僅在if分支中使用whats並丟棄else分支。 但它似乎沒有這樣做,因為我得到警告:

 warning: comparison between signed and unsigned integer expressions [-Wsign-compare]|

指向這一行。 但這不是我想要的。 任何想法如何告訴編譯器做正確的事情?

編譯器是:x86-64上的gcc 4.7.2,debian

謝謝!

這是警告,不是錯誤。 警告會警告您一些您可能不想做但違法的事情。 在這種情況下,您要這樣做,因此請忽略它。 您可以使用編譯指示來忽略該警告,但是我建議您記錄一下如何知道它對將來的開發人員是安全的。

禁用此警告的特定於GCC的編譯指示為:

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsign-compare"
setCarryFlag = ((MAX_OF(decltype(res)) - _b) < _a);
#pragma GCC diagnostic pop

這里的問題是編譯器正在編譯所有代碼,即使那些將變成死代碼的代碼,因為它沒有更好的了解。

我認為,經典的解決方案是 提供另一個模板參數,該參數的默認值為 std::is_signed<B::get()>::value ,然后專門處理該參數 *。 但是,由於您要測試兩個參數的簽名,因此情況將變得復雜。

另一個選擇可能只是在if條件內創建一個帶符號的變量並使用它。 這將簡單地繞過警告。

if (std::is_signed<decltype(res)>::value) {
    typename std::make_signed<decltype(res)>::type sres = res;
    // now use sres
}

* Xeo提醒我您不能對功能模板進行部分專業化,因此經典的解決方案實際上是標簽分派。

暫無
暫無

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

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