Search code examples
c++language-lawyersize-tptrdiff-t

Does size_t have the same size and alignment as ptrdiff_t?


On my platform (and on most of them I think) std::size_t and std::ptrdiff_t have the same size and the same alignment. Is there any platform where that is not true? In short: is it required by the standard?


Solution

  • In short: is it required by the standard?

    No. The only requirement is from [support.types.layout]/2 and it is:

    The type ptrdiff_­t is an implementation-defined signed integer type that can hold the difference of two subscripts in an array object, as described in [expr.add].

    There is paragraph 4

    [ Note: It is recommended that implementations choose types for ptrdiff_­t and size_­t whose integer conversion ranks are no greater than that of signed long int unless a larger size is necessary to contain all the possible values. — end note ]

    but notes are non-normative and it is only a recommendation, not a requirement.


    std::size_t is defined as

    The type size_­t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object ([expr.sizeof]).

    in paragraph 3 and it also has no requirement that they be the same.