offsetof
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <cstddef>
|
||
| #define offsetof(type, member) /*implementation-defined*/ |
||
宏offsetof扩展类型std::size_t,,它的值的偏移量,以字节为单位,从一个指定类型的对象的开始到其指定的部件,包括填充物,如果没有..恒定
Original:
The macro offsetof expands to a constant of type std::size_t, the value of which is the offset, in bytes, from the beginning of an object of specified type to its specified member, including padding if any.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目录 |
[编辑] 注释
type是不是一个标准的布局类型,其行为是未定义.Original:
If
type is not a standard-layout type, the behavior is undefined.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
member是一个静态成员或函数成员,该行为是未定义.Original:
If
member is a static member or a function member, the behavior is undefined.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
一个标准的布局类型的第一个成员的偏移量始终为零(空基的优化是强制性的)
Original:
The offset of the first member of a standard-layout type is always zero (空基的优化 is mandatory)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 可能的实现
#define offsetof(type,member) ((std::size_t) &(((type*)0)->member)) |
[编辑] 为例
Output:
the first element is at offset 0 the double is at offset 8
[编辑] 另请参阅
| 返回无符号整数类型的sizeof运营商 Original: unsigned integer type returned by the sizeof operator The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (的typedef) | |
| (C++11) |
检查如果一个类型是标准的布局类型 Original: checks if a type is standard-layout type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (类模板) |