[英]Why does bit-wise negate operator “~” cast to int? (conversion to ‘unsigned char’ from ‘int’ may alter its value)
GCC 4.9.1 reports "warning: conversion to 'unsigned char' from 'int' may alter its value [-Wconversion]" with the following code GCC 4.9.1报告“警告:从'int'转换为'unsigned char'可能会更改其值[-Wconversion]”,其代码如下
#include <cstdlib>
int main( int , char*[] ) {
unsigned char *dest = new unsigned char[16];
const unsigned char *src = new unsigned char[16];
for( size_t i = 0; i != 16; ++i) {
dest[i] = ~(src[i]);
}
return 0;
}
Obviously, both src
and dest
are both pointers to an array of unsigned char
and I only want the latter to be the bit-wise negation of the former. 显然,
src
和dest
都是指向unsigned char
数组的指针,我只希望后者成为前者的按位取反。 For some strange reason the ~
operator seems to return an int
and thus triggering the warning. 由于某些奇怪的原因,
~
运算符似乎返回一个int
,从而触发警告。 Why? 为什么? Is this intended behaviour?
这是预期的行为吗?
Of course, I know I could use a static_cast<unsigned char>()
to prevent the warning, but I feel that something else is wrong and the warning should not be there in the first place. 当然,我知道我可以使用
static_cast<unsigned char>()
来阻止警告,但是我觉得还有其他问题,并且警告不应该放在第一位。
I guess the literal answer is because the Standard says so. 我猜字面上的答案是因为标准这么说。 From [expr.unary.op]:
来自[expr.unary.op]:
The operand of
~
shall have integral or unscoped enumeration type;~
的操作数应具有整数或无范围的枚举类型; the result is the one's complement of its operand.结果是一个操作数的补码。 Integral promotions are performed.
进行整体促销。 The type of the result is the type of the promoted operand.
结果的类型是提升操作数的类型。
which, according to [conv.prom] is: 根据[conv.prom],它是:
A prvalue of an integer type other than
bool
,char16_t
,char32_t
, orwchar_t
whose integer conversion rank (4.13) is less than the rank ofint
can be converted to a prvalue of typeint
ifint
can represent all the values of the source type;如果
int
可以表示源类型的所有值,则整数转换等级(4.13)小于int
等级的bool
类型,bool
,char16_t
,char32_t
或wchar_t
的整数类型的prvalue可以转换为int
类型的prvalue。 ; otherwise, the source prvalue can be converted to a prvalue of typeunsigned int
.否则,可以将源prvalue转换为
unsigned int
类型的prvalue。
And int
has higher rank than unsigned char
. 并且
int
等级高于unsigned char
。 So yes, it's intended behavior, and an explicit static_cast
squelches the warning. 因此,是的,这是预期的行为,并且显式
static_cast
抑制了警告。
all operands in expressions are promoted to at least int
s, because int
should represent 'natural' integral type for given architecture. 表达式中的所有操作数至少都提升为
int
,因为int
应该表示给定体系结构的“自然”整数类型。
So this warning is proper - type on right side of assignment will be int
(for some people it might indicate place for optimization*), static_cast
is a good solution (I would add mask for 0xFF
to it, just to be sure and properly state my intent). 因此,此警告是正确的-赋值右侧的类型将为
int
(对于某些人来说,它可能指示优化的位置*), static_cast
是一个很好的解决方案(我将向其添加0xFF
掩码,只是为了确保正确地声明状态)我的意图)。
*) for example: one could negate 4 bytes at once, making better use of CPU. *)例如:一个可以一次取反4个字节,从而更好地利用CPU。 Some compilers might do it by them selfs.
一些编译器可能会自己完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.