[Bug c++/66170] Bogus warning with -Wsign-conversion when using static_cast on an int

2017-08-22 Thread egallager at gcc dot gnu.org
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66170

Eric Gallager  changed:

   What|Removed |Added

 Status|UNCONFIRMED |RESOLVED
 CC||egallager at gcc dot gnu.org
 Resolution|--- |DUPLICATE

--- Comment #2 from Eric Gallager  ---
Looks like bug 60342 to me.

*** This bug has been marked as a duplicate of bug 60342 ***

[Bug c++/66170] Bogus warning with -Wsign-conversion when using static_cast on an int

2016-10-13 Thread sir.vestnik at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66170

Sergey Vidyuk  changed:

   What|Removed |Added

 CC||sir.vestnik at gmail dot com

--- Comment #1 from Sergey Vidyuk  ---
I have similair issue with gcc-6.2

[vestnik@VestniK-laptop C++]$ cat fasttest.cpp 
#include 
#include 

void foo(const std::vector vec, const int idx) {
if (idx < 0 || vec.size() <= static_cast(idx))
{
std::cout << "No such index\n";
return;
}
std::cout << "Valid index\n";
}

int main() {
foo({1, 2, 3, 4 ,5}, 6);
return 0;
}
[vestnik@VestniK-laptop C++]$ g++ -std=c++11 -Wsign-conversion -Wall -Werror
fasttest.cpp -o fasttest
fasttest.cpp: In function 'void foo(std::vector, int)':
fasttest.cpp:5:31: error: conversion to 'std::vector::size_type {aka long
unsigned int}' from 'const int' may change the sign of the result
[-Werror=sign-conversion]
  if (idx < 0 || vec.size() <= static_cast(idx))
   ^~~~
cc1plus: all warnings being treated as errors
[vestnik@VestniK-laptop C++]$ g++ --version
g++ (GCC) 6.2.1 20160830
Copyright (C) 2016 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.


It looks like static_cast type was incorrectly treated as const int instead of
size_t. Changing static_cast(idx) to static_cast(idx)
silences diagnostics but such fix looks lihe a strange hack.