[Bug c++/93589] Template instantiation creates a conversion warning when it should not

jrdowning at yahoo dot com gcc-bugzilla@gcc.gnu.org
Sat Feb 15 00:02:00 GMT 2020


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

John Downing <jrdowning at yahoo dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jrdowning at yahoo dot com

--- Comment #5 from John Downing <jrdowning at yahoo dot com> ---
I'm not sure if this will be seen but here goes.

Try compiling the following with -Wconversion:

#include <iostream>
#include <iomanip>

#define CHAR_BIT 8

using std::cout;

void print_byte_order_short( short )
{
   unsigned short i;
   short val = 0;
   for(i = 1; i < sizeof(short); ++i)
   {
      val |= (CHAR_BIT * static_cast<short>(i)) << (CHAR_BIT *
static_cast<short>(i));
   }
   cout << val;
}

void print_byte_order_char( char )
{
   unsigned char i;
   char val = 0;
   for(i = 1; i < sizeof(char); ++i)
   {
      val |= (CHAR_BIT * static_cast<char>(i)) << (CHAR_BIT *
static_cast<char>(i));
   }
   cout << val;
}

void print_byte_order_long( long )
{
   unsigned long i;
   long val = 0;
   for(i = 1; i < sizeof(long); ++i)
   {
      val |= (CHAR_BIT * static_cast<long>(i)) << (CHAR_BIT *
static_cast<long>(i));
   }
   cout << val;
}

void print_byte_order_int( int )
{
   unsigned int i;
   int val = 0;
   for(i = 1; i < sizeof(int); ++i)
   {
      val |= (CHAR_BIT * static_cast<int>(i)) << (CHAR_BIT *
static_cast<int>(i));
   }
   cout << val;
}



int main()
{
  char c;
  short s;
  int i;
  long l;

   print_byte_order_char(c); 
   print_byte_order_short(s);
   print_byte_order_int(i);
   print_byte_order_long(l); 
}


This will generate a warning, for the char and short cases, when the "|="
happens.  This is despite "val" being explicitly declared as a char or an
short.

I can see this being correct if "val" has been declared as "unsigned", which i
shorthand for "unsigned int".  But if "val" is explicitly declared as
something, there should be a potential for the conversion to change the value.


More information about the Gcc-bugs mailing list