[Bug c++/59231] New: gcc misses [-Werror=sign-compare] when inside a template
a_mcgurn at yahoo dot co.uk
gcc-bugzilla@gcc.gnu.org
Thu Nov 21 12:43:00 GMT 2013
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59231
Bug ID: 59231
Summary: gcc misses [-Werror=sign-compare] when inside a
template
Product: gcc
Version: 4.8.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: a_mcgurn at yahoo dot co.uk
The following code:
----------- //filename
#include <iostream>
template<class X, class Y>
bool equals( X x, Y y )
{
return (x == y);
}
int main()
{
std::cout << "Hello World!" << std::endl;
size_t x = 2;
signed int y = 2;
if(!equals( x, y ))
{
std::cout << "Hello World again - x == y !" << std::endl;
}
return 0;
}
-----------
This produces for g++-4.8.2:
bfs-dl360g7-27$ g++ --std=c++11 -Wall helloWorld.cpp -o helloWorld
NO warnings
for g++-4.4.7 it correctly warns on the 'signed == unsigned long' as incorrect:
bfs-dl360g7-27$ /usr/bin/g++ -Wall helloWorld.cpp -o helloWorld
helloWorld.cpp: In function âbool equals(X, Y) [with X = long unsigned int, Y =
int]â:
helloWorld.cpp:16: instantiated from here
helloWorld.cpp:7: warning: comparison between signed and unsigned integer
expressions
----
However if comparison is not inside a template say direct comparison:
if(x == y)
then g++-4.8.2 will pick this up as a warning:
bfs-dl360g7-27$ g++ --std=c++11 -Wall -Werror testComparison.cpp -o
testComparison
testComparison.cpp: In function âint main()â:
testComparison.cpp:9:12: error: comparison between signed and unsigned integer
expressions [-Werror=sign-compare]
if(x == y)
so it appears to have difficulty seeing this issue if wrapped in a
template/macro.
bfs-dl360g7-27$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/gcc-4.8.2/bin/../libexec/gcc/x86_64-unknown-linux-gnu/4.8.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ./configure --prefix=/opt/gcc-4.8.2 --enable-languages=c,c++
--with-mpc=/opt/gcc-4.8.2 --with-mpfr=/opt/gcc-4.8.2 --with-gmp=/opt/gcc-.8.2
--with-cloog=/opt/gcc-4.8.2 --with-isl=/opt/gcc-4.8.2
Thread model: posix
gcc version 4.8.2 (GCC)
preprocessed file attached -
apologies if i've missed anything vital or a bug already raised.
rgds
More information about the Gcc-bugs
mailing list