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 ofalignof
, 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?
alignof
to turn into a runtime thingy when used on a variable name (I guess this is what the compiler extension does). I didn't expect everything to be known at compile-time. – Ganleyfloat
array is suitably aligned to be used with SSE. – Ganley&
, but bitwise-and cannot be used on a pointer – Heavenfloat
to be 16 byte aligned, an array offloat
that is 32 byte aligned is also suitable SSE if I'm not mistken. – Ganley