简体   繁体   English

c ++位字段和-Wconversion

[英]c++ bit fields and -Wconversion

-Wconversion is producing warnings when I assign a value to a bit field with g++. 当我用g ++为一个位字段赋值时, -Wconversion会产生警告。

Source file: 源文件:

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 output: 编译输出:

$ 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]

I want -Wconversion enabled for other parts of my project (including within this file). 我想为项目的其他部分启用-Wconversion (包括在此文件中)。 How do I "fix" the assignment statements here so I don't get warnings? 如何在此处“修复”赋值语句,以便我不会收到警告?

Ensure the conversion can't overflow. 确保转换不会溢出。 Here's one way: 这是一种方式:

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);
}

Wconversion will give warning every time there is a possibly that your implicit conversion could alter your value . 每次有可能您的隐式转换可能会改变您的价值时,Wconversion会发出警告。 That been said, there's no problem with you code. 话虽如此,代码没有问题。

Using static_cast<usigned_int> as it's been mention above will fix your issue. 正如上面提到的那样使用static_cast<usigned_int>将解决您的问题。

Some more information 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM