[英]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.