assert that struct type is declared with alignas()

317 Views Asked by At

As the title states, I need some way to assert that a type has been declared with alignas as such:

struct alignas(16) MyStruct {
    ...
};

It is meant to be used for a template parameter where the template class needs to make sure that the type it is templated on is 16 byte aligned.

1

There are 1 best solutions below

1
On BEST ANSWER

There is alignof that you can use to make sure the type you get is aligned to the correct size. You would use it in your function like

template <typename T>
void foo(const T& bar)
{
    static_assert(alignof(T) == 16, "T must have an alignment of 16");
    // rest of function
}

If using this in a class you'd have

template <typename T>
class foo
{
    static_assert(alignof(T) == 16, "T must have an alignment of 16");
    // rest of class
};