0

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.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Tegon McCloud
  • 63
  • 1
  • 6

1 Answers1

4

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
};
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • You may as well just go straight to `alignof`, save yourself a lot of typing. `alignof` was introduced at the same time as `alignas`. – Raymond Chen Dec 19 '19 at 22:54