在C ++ 14中的有符号位字段 [英] Signed bit field in C++14
问题描述
我相信,直到C ++ 14,声明为 int
的结构的位字段仍然被解释为 signed
或 unsigned
,解释是实现定义的。参考: http://en.cppreference.com/w/cpp/language/bit_field。
I believe that until C++14 a bit field of a struct declared as int
was still interpreted as either signed
or unsigned
, the interpretation being implementation defined. Reference: http://en.cppreference.com/w/cpp/language/bit_field.
在C ++ 14中还是这样吗?也就是说,下面的代码是否保证可以正常工作?
Is this still the case in C++14? I.e., is the code below guaranteed to work as inteded?
#include <iostream>
struct X
{
int f:3;
};
int main()
{
X x;
x.f = -2; // is this going to be indeed signed? It seems so.
std::cout << x.f << std::endl; // displays -2
}
推荐答案
到C ++ 11标准§9.6/ p3位字段[class.bit] (强调Mine ):
According to C++11 standard §9.6/p3 Bit-fields [class.bit] (Emphasis Mine):
位字段不应该是静态成员。位字段必须具有
整数或枚举类型(3.9.1)。 它是实现定义的
是否是一个简单(既不明确签名也不是无符号) char
, short
,
int
, long
或 long long
位字段是有符号或无符号。 A bool
value
可以成功存储在任何非零大小的位字段中。
地址的运算符&不应用于位字段,因此
不是位字段的指针。非const引用不应将
绑定到位字段(8.5.3)。 [注意:如果
引用的初始化器类型为const T&是引用位字段的左值,引用
绑定到临时初始化以保存
位字段的值;该引用不直接绑定到位域。请参阅
8.5.3。 - end note]
A bit-field shall not be a static member. A bit-field shall have integral or enumeration type (3.9.1). It is implementation-defined whether a plain (neither explicitly signed nor unsigned)
char
,short
,int
,long
, orlong long
bit-field is signed or unsigned. Abool
value can successfully be stored in a bit-field of any nonzero size. The address-of operator & shall not be applied to a bit-field, so there are no pointers to bitfields. A non-const reference shall not be bound to a bit-field (8.5.3). [ Note: If the initializer for a reference of type const T& is an lvalue that refers to a bit-field, the reference is bound to a temporary initialized to hold the value of the bit-field; the reference is not bound to the bit-field directly. See 8.5.3. —end note ]
所以你对第一部分是正确的。事实上,直到C ++ 14,声明为的结构的位域仍然被解释为
signed
或<$
So you're correct for the first part. Indeed until C++14 a bit field of a struct declared as signed
was still interpreted as either signed
or unsigned
, the interpretation being implementation defined.
As already mentioned in this comments by @T.C. Defect reports referring to the issue were made DR739, DR675. Resulting in the following resolutions in C++14 standard:
措辞是由实现定义的,无论是明文签名还是无签名 char
, short
, int
, long
或 long long
位字段是有符号或无符号。,被删除,现在的C ++ 14措辞是:
The wording "It is implementation-defined whether a plain (neither explicitly signed nor unsigned) char
, short
, int
, long
, or long long
bit-field is signed or unsigned.", was removed, and the C++14 wording now is:
位字段不应该是静态成员。位字段必须具有
整数或枚举类型(3.9.1)。 bool值可以成功地存储在任何非零大小的位字段中的
。运营商和运营商的地址。
不应用于位字段,因此没有指向
位字段的指针。非const引用不应该绑定到位字段
(8.5.3)。 [注意:如果const T&
是一个引用位字段的左值,引用绑定到一个
临时初始化来保存位字段的值;
引用不直接绑定到位域。见8.5.3。 -end note
]
A bit-field shall not be a static member. A bit-field shall have integral or enumeration type (3.9.1). A bool value can successfully be stored in a bit-field of any nonzero size. The address-of operator & shall not be applied to a bit-field, so there are no pointers to bit-fields. A non-const reference shall not be bound to a bit-field (8.5.3). [ Note: If the initializer for a reference of type const T& is an lvalue that refers to a bit-field, the reference is bound to a temporary initialized to hold the value of the bit-field; the reference is not bound to the bit-field directly. See 8.5.3. —end note ]
也在§C.1.8第9条:classes [diff.class] 添加了以下部分:
9.6
9.6
更改:类型为plain int的位字段已签名。
Change: Bit-fields of type plain int are signed.
基本原理
模板特殊化的不一致定义。为了一致性,对于非依赖的
类型,也消除了实现自由。
Rationale: Leaving the choice of signedness to implementations could lead to inconsistent definitions of template specializations. For consistency, the implementation freedom was eliminated for non-dependent types, too.
对原始特征的影响:
转换难度:语法转换。
使用范围:很少。
strong>类型为 int
的位字段已签名,并且发布的代码可保证按预期工作。
Consequently, in C++14 bit-fields of type plain int
are signed and the code posted is guaranteed to work as intended.
这篇关于在C ++ 14中的有符号位字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!