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. |
| Заголовочный файл <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.
Содержание |
[править] Notes
Если
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)) |
[править] Пример
Вывод:
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. (определение типа) | |
| (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. (шаблон класса) |