xte::ptr_diff()

Defined in header <xte/data/ptr_diff.hpp>

Computes the absolute difference between two pointers belonging to one object, avoiding signed integer wrapping.


Definition

[[nodiscard]] constexpr xte::uz ptr_diff(auto* begin, decltype(begin) end, xte::uz size) noexcept;
begin must point to the start of the object, and size must be the size of the object to which the pointers belong.



Example

constexpr char data[] = "hello";
static_assert(xte::ptr_diff(data, data + 3, sizeof(data)) == 3);
[View in Compiler Explorer]