2012-04-17 15 views
7

-Wconversion produce avvisi quando assegno un valore a un campo di bit con g ++. Il file sorgentecampi C++ bit e -Wconversion

: uscita

struct Foo 
{ 
public: 
    unsigned int x : 4; 
    unsigned int y : 9; 
    unsigned int z : 17; 
}; 

int main(int, char**) 
{ 
    int a = 12; 
    Foo f; 
    f.x = a; 
    f.x = (unsigned int)a; 
    f.x = (unsigned char)a; 
    f.x = (unsigned short)a; 
    f.x = (unsigned)a; 

    f.y = a; 
    f.y = (unsigned int)a; 
    f.y = (unsigned char)a; // no warning, sizeof(char) < 9 
    f.y = (unsigned short)a; 
    f.y = (unsigned)a; 

    f.z = a; 
    f.z = (unsigned int)a; 
    f.z = (unsigned char)a; // no warning, sizeof(char) < 17 
    f.z = (unsigned short)a; // no warning, sizeof(char) < 17 
    f.z = (unsigned)a; 
} 

Compilation:

$ g++ --version 
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 
<snip> 
$ g++ -Wconversion test.cpp 
test.cpp: In function ‘int main(int, char**)’: 
test.cpp:13:8: warning: conversion to ‘unsigned char:4’ from ‘unsigned int’ may alter its value [-Wconversion] 
test.cpp:14:22: warning: conversion to ‘unsigned char:4’ from ‘unsigned int’ may alter its value [-Wconversion] 
test.cpp:15:23: warning: conversion to ‘unsigned char:4’ from ‘unsigned char’ may alter its value [-Wconversion] 
test.cpp:16:24: warning: conversion to ‘unsigned char:4’ from ‘short unsigned int’ may alter its value [-Wconversion] 
test.cpp:17:18: warning: conversion to ‘unsigned char:4’ from ‘unsigned int’ may alter its value [-Wconversion] 
test.cpp:19:8: warning: conversion to ‘short unsigned int:9’ from ‘unsigned int’ may alter its value [-Wconversion] 
test.cpp:20:22: warning: conversion to ‘short unsigned int:9’ from ‘unsigned int’ may alter its value [-Wconversion] 
test.cpp:22:24: warning: conversion to ‘short unsigned int:9’ from ‘short unsigned int’ may alter its value [-Wconversion] 
test.cpp:23:18: warning: conversion to ‘short unsigned int:9’ from ‘unsigned int’ may alter its value [-Wconversion] 
test.cpp:25:8: warning: conversion to ‘unsigned int:17’ from ‘unsigned int’ may alter its value [-Wconversion] 
test.cpp:26:22: warning: conversion to ‘unsigned int:17’ from ‘unsigned int’ may alter its value [-Wconversion] 
test.cpp:29:18: warning: conversion to ‘unsigned int:17’ from ‘unsigned int’ may alter its value [-Wconversion] 

voglio -Wconversion abilitato per altre parti del mio progetto (compreso all'interno di questo file). Come faccio a "correggere" le dichiarazioni di assegnazione qui in modo da non ricevere avvisi?

+0

Che cosa succede se si prende il 'int' off? –

+0

@ 0A0D vedi aggiornamenti. – robert

risposta

11

Assicurarsi che la conversione non possa traboccare. Ecco un modo:

struct Foo 
{ 
public: 
    unsigned int x : 4; 
    unsigned int y : 9; 
    unsigned int z : 17; 
}; 

int main(int, char**) 
{ 
    int a = 12; 
    Foo f; 
    f.x = static_cast<unsigned int>(a & 15);  
    f.y = static_cast<unsigned int>(a & 511); 
    f.z = static_cast<unsigned int>(a & 131071); 
} 
+0

Questo funziona. Grazie. – robert

-1

Wconversion darà avviso ogni volta che c'è un forse che il vostro conversione implicita potrebbe alterare il valore del. Detto questo, non c'è nessun problema con il tuo codice.

L'utilizzo di static_cast<usigned_int> come indicato sopra risolverà il problema.

Some more information