https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66170

            Bug ID: 66170
           Summary: Bogus warning with -Wsign-conversion when using
                    static_cast<size_t> on an int
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yaghmour.shafik at gmail dot com
  Target Milestone: ---

Given the following code:

#include <cstring>

int main()
{   
    int length = 0;
    size_t n;

    n = static_cast<size_t>(length) ;                     // Line 8
    n = static_cast<size_t>(length) * sizeof(int) ;
    n = sizeof(int) * static_cast<long unsigned int>(length) ;   
    n = sizeof(int) * static_cast<unsigned int>(length) ; // Line 11

    n = sizeof(int) * static_cast<size_t>(length) ;       // Line 13
}

building with gcc 4.9.2 or greater using the following command line:

  g++ -Wsign-conversion 

generates the following warning on line 13:

  warning: conversion to 'long unsigned int' from 'int' may change the sign of
the result [-Wsign-conversion]

This looks like an error to me, the documentation for -Wsign-conversion says:

  An explicit cast silences the warning

and line 8 to line 11 are very similar but do not generate any warning.

Optimization level does not make a difference and -Wall -Wextra does not
indicate additional issues.

Reply via email to