Must size() == end() - begin()? What about the cast?

后端 未结 2 1161
面向向阳花
面向向阳花 2021-02-12 14:58

From what I understand, the purpose of size_type and difference_type is not merely the sign -- it was also meant to address e.g. segme

2条回答
  •  时光取名叫无心
    2021-02-12 15:12

    I don't think it is always safe. This historical issue existed since first specification of C language, where ptrdiff_t is not guaranteed to cover the entire positive range of size_t. For obvious reasons, this issue carries over to the specification of std::vector.

    With standard C++ containers, it is guaranteed that size_type covers the non-negative range of difference_type, but the reverse coverage is not guaranteed.

    However, the relationship between size() and end() - begin() for a standard container can be guaranteed by other means. The implementation is free to impose its own limitations on the maximum container size, which are exposed through container::max_size() function. It can artificially restrict the maximum size to make sure the subtraction never overflows.

    P.S. I'd say that the reason for difference_type's existence is just the sign and nothing else. To be completely "safe" difference_type should be 1 bit longer than size_type. This is often difficult to achieve in practice, which is why it is not required by the language specification.

提交回复
热议问题