You can try with something like:
bool is_aligned(const volatile void *p, std::size_t n)
{
return reinterpret_cast<std::uintptr_t>(p) % n == 0;
}
assert(is_aligned(array, 16));
The above assumes a flat address space and that arithmetic on uintptr_t
is equivalent to arithmetic on char *
.
While these conditions prevail for the majority of modern platforms, neither of which is required by the standard.
It’s entirely possible for an implementation to perform any transformation when casting void *
to uintptr_t
as long the transformation can be reversed when casting back from uintptr_t
to void *
(see What is uintptr_t data type).
Further details in N4201 (it proposes, among other things, the is_aligned()
operation).
EDIT
is
volatile
necessary here?
It allows something like:
alignas(16) volatile float a;
assert(is_aligned(&a, 16));
Without volatile
you get the error
no known conversion from ‘volatile float *’ to ‘const void *’ for 1st argument
Further references:
- Why and when is cast to char volatile& needed?
- Why is a point-to-volatile pointer, like “volatile int * p”, useful?