warning: conversion to ‘unsigned int:31’ from ‘unsigned int’ may alter its value [-Wconversion]

Paweł Sikora pluto@agmk.net
Wed Aug 28 12:11:00 GMT 2013


Hi,
 
i'd like to add -Wconversion to my build flags but it warns for (smaller type) bitfields assignments.
manual says that it shouldn't warn for explicit casts but there're any notes about explicit casts
to smaller bitfield types. finally, the current gcc-4.8.x reports a warning:
 
struct X { unsigned field : 31; };
int main()
{
        unsigned u = 0u;
        X x = { .field = static_cast< typeof( X::field ) >( u ) };
        return x.field;
}
 
 
$ g++ u.cpp -O2 -c -Wall -Wconversion
u.cpp: In function ‘int main()’:
u.cpp:6:58: warning: conversion to ‘unsigned int:31’ from ‘unsigned int’ may alter its value [-Wconversion]
  X x = { .field = static_cast< typeof( X::field ) >( u ) };
                                                          ^
 
is it a bug in gcc or an improper cast from my side?
 
BR,
Paweł.



More information about the Gcc-help mailing list