是的sizeof(T)==的sizeof(int)的? [英] Is sizeof(T) == sizeof(int)?
问题描述
我一直在钻研的标准草案,似乎无法找到我要找的。 P>
如果我有一个标准布局类型。
结构T {
无符号的手柄;
};
后来我知道, reinter pret_cast<无符号*>(& T公司)==&安培; t.handle
一些 T(T);
我们的目标是创建一些矢量< T> v
并通过&放大器;五[0]
C函数期望一个指向无符号整数数组
因此,没有标准的定义的sizeof(T)==的sizeof(无符号)
及这是否意味着 T的数组
将有相同布局的数组无符号
?
问题解决了一个非常类似的话题,我米询问其中两个数据成员和类是标准布局的特定情况下,与数据成员是一个基本的类型。
我读过一些段落,似乎暗示的也许的可能是真实的,但没有什么击中了要害。例如:
第9.2.17
两个标准布局结构(第9条)类型是布局兼容,如果
它们具有相同数量的非静态数据成员和相应的
非静态数据成员(按照声明顺序)具有布局兼容
类型
块引用>这是并不完全符合我要找的,我不认为。
解决方案您基本上都在问,给出:
结构T {
ü处理;
};不管是保证
的sizeof(T)==的sizeof(U)
。不,不是。在ISO C ++标准03第9.2 / 17说道:
一个指向POD结构对象,适当使用转换
reinter pret_cast
,指出了其最初的成员(如该成员是
位字段,然后在它所在的单元),反之亦然。
块引用>假设你有
的结构T
的数组。该反之亦然部分意味着任何一个结构T $ C的
T ::处理
成员也必须是一个有效的地址的地址$ C>。现在,假设这些成员的类型的字符
和你的说法是真实的。这意味着结构T
将被允许有一个未对齐的地址,这似乎是相当不可能的。标准通常设法不配合实现的手中以这样的方式。对于你所说的是真实的,该标准将不得不要求结构T
允许有不对齐的地址。而且它必须被允许用于所有结构,因为结构T
可能是一个前瞻性声明的,不透明的类型。此外,部分9.2 / 17继续说:
[注:因此,有可能是一个POD结构对象中未命名的填充,而不是在它的开始,因为要达到相应的调整]
块引用>其中,采取了不同的方式,意味着不保证绝不会有填充
I've been poring over the draft standard and can't seem to find what I'm looking for.
If I have a standard-layout type
struct T { unsigned handle; };
Then I know that
reinterpret_cast<unsigned*>(&t) == &t.handle
for someT t;
The goal is to create some
vector<T> v
and pass&v[0]
to a C function that expects a pointer to an array of unsigned integers.So, does the standard define
sizeof(T) == sizeof(unsigned)
and does that imply that an array ofT
would have the same layout as an array ofunsigned
?While this question addresses a very similar topic, I'm asking about the specific case where both the data member and the class are standard layout, and the data member is a fundamental type.
I've read some paragraphs that seem to hint that maybe it might be true, but nothing that hits the nail on the head. For example:
§ 9.2.17
Two standard-layout struct (Clause 9) types are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in declaration order) have layout-compatible types
This isn't quite what I'm looking for, I don't think.
解决方案You essentially are asking, given:
struct T { U handle; };
whether it's guaranteed that
sizeof(T) == sizeof(U)
. No, it is not.Section 9.2/17 of the ISO C++03 standard says:
A pointer to a POD-struct object, suitably converted using a
reinterpret_cast
, points to its initial member (or if that member is a bit-field, then to the unit in which it resides) and vice versa.Suppose you have an array of
struct T
. The vice versa part means that the address of any of theT::handle
members must also be a valid address of astruct T
. Now, suppose that these members are of typechar
and that your claim is true. This would mean thatstruct T
would be allowed to have an unaligned address, which seems rather unlikely. The standard usually tries to not tie the hands of implementations in such a way. For your claim to be true, the standard would have to require thatstruct T
be allowed to have unaligned addresses. And it would have to be allowed for all structures, becausestruct T
could be a forward-declared, opaque type.Furthermore, section 9.2/17 goes on to state:
[Note: There might therefore be unnamed padding within a POD-struct object, but not at its beginning, as necessary to achieve appropriate alignment.]
Which, taken a different way, means that there is no guarantee that there will never be padding.
这篇关于是的sizeof(T)==的sizeof(int)的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!