offsetof
Da 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. |
Definido no cabeçalho <cstddef>
|
||
#define offsetof(type, member) /*implementation-defined*/ |
||
O offsetof macro expande para uma constante de std::size_t tipo, cujo valor é o deslocamento, em bytes, a partir do início de um objecto do tipo especificado para o seu membro especificado, incluindo estofamento, se qualquer.
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.
Índice |
[editar] Notas
Se
type
não é um tipo padrão de layout, o comportamento é indefinido.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.
Se
member
é um membro estático ou um membro da função, o comportamento é indefinido.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.
O deslocamento do primeiro membro de um tipo padrão de layout é sempre zero (empty-base de otimização é obrigatório)
Original:
The offset of the first member of a standard-layout type is always zero (empty-base de otimização 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.
[editar] Possível implementação
#define offsetof(type,member) ((std::size_t) &(((type*)0)->member)) |
[editar] Exemplo
#include <iostream> #include <cstddef> struct S { char c; double d; }; int main() { std::cout << "the first element is at offset " << offsetof(S, c) << '\n' << "the double is at offset " << offsetof(S, d) << '\n'; }
Saída:
the first element is at offset 0 the double is at offset 8
[editar] Veja também
tipo inteiro sem sinal retornado pelo operador 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) |
verifica se um tipo é o layout padrão tipo 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. (modelo de classe) |