开发者

int(int(-2)/unsigned(2)) = 2147483647 no warning

开发者 https://www.devze.com 2023-04-09 15:30 出处:网络
I have a code similar to this: template<typename Ta, typename Tb> Ta doStuff(Ta a, Tb b) { ... return a/b;

I have a code similar to this:

template<typename Ta, typename Tb> Ta doStuff(Ta a, Tb b)
{
    ...
    return a/b;
}

As the title says such code would return wrong values with Ta=int Tb=unsigned.

Is there a way to get a warning 开发者_JAVA百科by g++ for this case ?


Yes. Use -Wsign-conversion option:

[nawaz@./]$ g++ filename.cpp -Wsign-conversion


Try with: g++ -Wall code.cpp -o output

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号