C++11 introduced the alignas
specifier to specify the alignment of a variable, and the alignof
operator to query the default alignment of a type. However, I don't see any way to get the alignment of a specific variable. Let's take the following trivial example:
alignas(16) float* array;
Here is what we can do about it:
alignof(float*)
returns 8, which is obviously not what we want.alignof(array)
returns 16, which is exactly what we want, but that's a compiler extension; alignof
as specified by the standard can't be used on a specific variable.alignof(decltype(array))
returns 8, which was quite expected but not what we want.std::alignment_of
is implemented in terms of alignof
, so it doesn't help much.I would like a mechanism to confirm that the specific variable array
is aligned on a 16 byte boundary. Is there anything in the standard to perform such a query?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With