简体   繁体   English

g ++不会在模板代码中发出-Wsign-compare

[英]g++ doesn't issue -Wsign-compare in template code

I recently noticed that g++ doesn't issue signed/unsigned comparison warning when the offending code is in a function template. 我最近注意到,当有问题的代码位于函数模板中时,g ++不会发出有符号/无符号比较警告。 Here is a sample: 这是一个示例:

// signed_unsigned.cc
#include <cassert>
#include <string>

template<typename T, typename U>
bool compare(T t, U u) {
    return t >= u;
}

int main(int argc, char** argv)
{
    size_t x = strtoul(argv[1], 0, 0);
    int y = strtol(argv[2], 0, 0);
    // bool chk = (x >= y);   // if I use this statement instead, it throws [-Wsign-compare] warning
    bool chk = compare(x, y);
    assert(chk);
    return 0;
}

And I'm compiling and executing it like this: 我正在像这样编译和执行它:

$ g++ -std=gnu++11 signed_unsigned.cc -Wall -Wsign-compare
$ ./a.out 0 -5
a.out: signed_unsigned.cc:15: int main(int, char**): Assertion `chk' failed.
Aborted (core dumped)

The assertion failure is expected as the integer promotion will convert -5 to really large unsigned value. 断言失败是预期的,因为整数提升会将-5转换为真正的大无符号值。 But the compilation should have issued warning about this comparison, No? 但是汇编应该对此比较发出警告,不是吗?

I might be missing something basic here but I searched online and couldn't find anything relevant. 我可能在这里缺少一些基本知识,但是我在网上搜索了却找不到任何相关信息。 Does anybody know why the template version of comparison doesn't throw warning? 有人知道为什么比较的模板版本不会发出警告吗?

GCC version used: 使用的GCC版本:

$ g++ --version
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Does anybody know why the template version of comparison doesn't throw warning? 有人知道为什么比较的模板版本不会发出警告吗?

It is probably a bug (quality of implementation issue) in that version of GCC. 该版本的GCC可能是一个错误(实现质量问题)。 GCC 5.5.0 for example does not fail to issue a diagnostic for the example program, so the issue seems to have been fixed in later versions. 例如,GCC 5.5.0不会对示例程序发出诊断,因此该问题似乎在更高版本中已得到解决。

The assertion failure is expected as the integer promotion 断言失败预期为整数提升

To be pedantic, this conversion is not classified as integer promotion. 要学究,此转换未归类为整数提升。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 -gsign-compare警告用g ++表示 - -Wsign-compare warning in g++ gcc警告-Wsign-compare在比较const时似乎不起作用 - gcc warning -Wsign-compare doesn't seem to work when comparing const 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] g ++不喜欢模板方法链接模板var吗? - g++ doesn't like template method chaining on template var? g ++可变参数模板问题 - g++ variadic template issue C++ 错误 - 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - C++ Error - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 我的 For 循环有什么问题? 我收到警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - What is wrong with my For loops? i get warnings: comparison between signed and unsigned integer expressions [-Wsign-compare] 为什么下面的代码不能在 MSVC 中编译而在 g++ 中编译? - Why the following code doesn't compile in MSVC and does in g++? G ++为未使用的模板特化生成代码? - G++ generates code for unused template specializations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM