【发布时间】:2019-09-15 23:05:20
【问题描述】:
我最近注意到,当违规代码位于函数模板中时,g++ 不会发出有符号/无符号比较警告。这是一个示例:
// 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;
}
我正在像这样编译和执行它:
$ 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)
预计断言失败,因为整数提升会将 -5 转换为非常大的无符号值。但是编译应该对这个比较发出警告,不是吗?
我可能在这里遗漏了一些基本的东西,但我在网上搜索并找不到任何相关的东西。有谁知道为什么模板版本的比较不会引发警告?
使用的 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.
【问题讨论】:
标签: c++ g++ gcc-warning g++4.8