Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      offsetof

      From cppreference.com
      <cpp‎ |types
       
       
      Utilities library
       
       
      Defined in header<cstddef>
      #define offsetof(type, member) /* implementation-defined */

      The macrooffsetof expands to an integral constant expression of typestd::size_t, the value of which is the offset, in bytes, from the beginning of an object of specified type to its specified subobject, includingpadding bits if any.

      Given an objecto of typetype and static storage duration,o.member shall be an lvalue constant expression that refers to a subobject ofo. Otherwise, the behavior is undefined. Particularly, ifmember is astatic data member, abit-field, or amember function, the behavior is undefined.

      Iftype is not aPODType(until C++11)standard-layout type(since C++11),the result ofoffsetof is undefined(until C++17)use of theoffsetof macro is conditionally-supported(since C++17).

      The expressionoffsetof(type, member) is nevertype-dependent and it is value-dependent if and only iftype is dependent.

      Contents

      [edit]Exceptions

      offsetof throws no exceptions.

      The expressionnoexcept(offsetof(type, member)) always evaluates totrue.

      (since C++11)

      [edit]Notes

      The offset of the first member of a standard-layout type is always zero (empty-base optimization is mandatory).

      (since C++11)

      offsetof cannot be implemented in standard C++ and requires compiler support:GCC,LLVM.

      member is not restricted to a direct member. It can denote a subobject of a given member, such as an element of an array member. This is specified byC DR 496.

      It is specified in C23 that defining a new type containing an unparenthesized comma inoffsetof is undefined behavior, and such usage is generally not supported by implementations in C++ modes:offsetof(struct Foo{int a, b;}, a) is rejected by all known implementations.

      [edit]Example

      Run this code
      #include <cstddef>#include <iostream> struct S{char   m0;double m1;short  m2;char   m3;//  private: int z; // warning: 'S' is a non-standard-layout type}; int main(){std::cout<<"offset of char   m0 = "<< offsetof(S, m0)<<'\n'<<"offset of double m1 = "<< offsetof(S, m1)<<'\n'<<"offset of short  m2 = "<< offsetof(S, m2)<<'\n'<<"offset of char   m3 = "<< offsetof(S, m3)<<'\n';}

      Possible output:

      offset of char   m0 = 0offset of double m1 = 8offset of short  m2 = 16offset of char   m3 = 18

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 273C++98offsetof may not work if unaryoperator& is overloadedrequired to work correctly even
      ifoperator& is overloaded
      LWG 306C++98the behavior was not specified whentype is not aPODTypethe result is undefined in this case
      LWG 449C++98other requirements ofoffsetof were
      removed by the resolution ofLWG issue 306
      added them back

      [edit]See also

      unsigned integer type returned by thesizeof operator
      (typedef)[edit]
      checks if a type is astandard-layout type
      (class template)[edit]
      C documentation foroffsetof
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/offsetof&oldid=170431"

      [8]ページ先頭

      ©2009-2025 Movatter.jp