offsetof
De 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. |
Déclaré dans l'en-tête <cstddef> | ||
#define offsetof(type, member) /*implementation-defined*/ | ||
offsetof la macro s'étend à une constante de std::size_t type, la valeur de ce qui est le décalage en octets du début d'un objet du type spécifié à son élément de donnée, y compris le cas échéant rembourrage .
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.
Sommaire |
[modifier]Notes
Si
type
n'est pas un type standard mise en page, le comportement est indéfini .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.
Si
member
est un membre statique ou un membre de la fonction, le comportement est indéfini .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.
Le décalage du premier membre d'un type standard-layout est toujours zéro (vide de base de l'optimisation est obligatoire)
Original:
The offset of the first member of a standard-layout type is always zero (vide de base de l'optimisation 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.
[modifier]Mise en œuvre possible
#define offsetof(type,member) ((std::size_t) &(((type*)0)->member)) |
[modifier]Exemple
#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';}
Résultat :
the first element is at offset 0 the double is at offset 8
[modifier]Voir aussi
unsigned integer retournée par l'opérateur 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) | vérifie si un type est standard disposition type 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. (classe générique) |