offsetof
De cppreference.com
<metanoindex/>
<tbody> </tbody>| 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.
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.
Possível implementação
#define offsetof(type,member) ((std::size_t) &(((type*)0)->member))
|
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
