【问题标题】:strange compiler warnings of C++11 Type checking codeC++11 类型检查代码的奇怪编译器警告
【发布时间】:2013-03-12 21:15:23
【问题描述】:

我正在编写一个带有进位/溢出检查的通用加法器,并且大量使用了 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 应该是真的,所以编译器应该只使用 if-branch 中的 whats 并丢弃 else 分支。 但它似乎没有这样做,因为我收到警告:

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

指向这条线。但这不是我想要的。 任何想法如何告诉编译器做正确的事情?

编译器是:gcc 4.7.2 on x86-64, debian

谢谢!

【问题讨论】:

  • 哪个是“MARKED LINE”?
  • 代码中有一条注释,包含这个流行语:{ //得到一个加法与结果的差值最高直到进位 /* MARKED LINE: 这个分支被错误检查 */ if (!std:: is_signed::value) setCarryFlag = ((MAX_OF(decltype(res)) - _b)
  • 好吧,我相信编译器会在优化之前进行这些检查。
  • 警告发生是因为代码仍在编译,即使优化器可以判断它是死代码。
  • “我大量使用 c++11 类型检查功能” 结合宏......我很失望。然后宏甚至不引用它们的参数。宏甚至不应该存在。为什么。我什至没有。

标签: c++ templates c++11 gcc-warning typetraits


【解决方案1】:

这是一个警告,而不是错误。警告用于警告您可能不想做但合法的事情。在这种情况下,你想这样做,所以忽略它。您可以使用编译指示忽略该警告,但我建议记录您如何知道它对未来的开发人员是安全的。

禁用此警告的 GCC 特定编译指示是:

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

【讨论】:

  • 我正在与 -Werr 合作,这不应更改,因此警告 == 错误。
  • @user1159208, "医生,医生,我这样做会很痛" "那就别那样做"
【解决方案2】:

这里的问题是编译器正在编译所有代码,即使是那些将成为死代码的代码,因为它不知道更好。

我相信经典的解决方案是提供另一个模板参数,其默认值为std::is_signed&lt;B::get()&gt;::value,然后专门处理该参数*。但是由于您要测试两个参数的签名性,这会变得很复杂。

另一个选项可能只是在 if 条件中创建一个有符号变量并使用它。这将简单地绕过警告。

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

*Xeo 提醒我,你不能部分特化函数模板,所以经典的解决方案实际上是标签调度。

【讨论】:

  • 经典的解决方案实际上是进行标签调度。您不能将函数模板专门化。此外,“解决方案”是无意义的。如果res 已经签名,为什么还要创建它的另一个签名版本?
  • @Xeo:我们正在创建它的签名版本,因此当res 实际上是未签名时编译器不会抱怨,这是这里的根本问题。 res 是无符号的,所以if 条件是0,但编译器仍然必须编译 if 的主体,这是警告的来源。通过制作签名版本,我们基本上是在告诉编译器编译这段代码,就好像它已经签名一样。
  • 标签调度意味着调用一个在std::true_typestd::false_type(例如)上重载的新函数f,并调用f(other, args, std::is_signed&lt;T&gt;());。编译器只会看到相应的重载,而不会抱怨另一个。这样,签名问题也解决了。
  • @Xeo:如果这里只有一个变量在起作用,标签分派就可以了。但他实际上是在测试BRES 的签名,所以你建议他写4次相同的函数吗?那么如果他还需要测试A 的签名会发生什么?现在是该功能的 8 个版本。这就是为什么make_signed&lt;&gt; 技巧很有吸引力的原因。
  • Xeo:我没有真正理解标签调度的意义。您能否进一步解释一下或提供一些参考链接?谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 2023-01-24
  • 2021-11-16
  • 1970-01-01
相关资源
最近更新 更多