Introduce an offset_ptr function to avoid quirky C pointer arithmetic
This commit is contained in:
parent
cb88630d86
commit
3815f9aa9f
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user