Introduce an offset_ptr function to avoid quirky C pointer arithmetic

This commit is contained in:
apio 2022-11-20 15:12:18 +01:00
parent cb88630d86
commit 3815f9aa9f

View File

@ -36,4 +36,11 @@ template <typename T> constexpr T get_blocks_from_size(T value, T block_size)
static_assert(get_blocks_from_size(40960, 4096) == 10);
static_assert(get_blocks_from_size(194, 64) == 4);
static_assert(get_blocks_from_size(2, 32) == 1);
static_assert(get_blocks_from_size(0, 256) == 0);
static_assert(get_blocks_from_size(0, 256) == 0);
// Offset a pointer by exactly <offset> bytes, no matter the type. Useful to avoid the quirks that come from C pointer
// arithmetic.
template <typename T, typename Offset> constexpr T* offset_ptr(T* ptr, Offset offset)
{
return (T*)((char*)ptr + offset);
}