1

The std::aligned_storage structure provides a type typedef that at least according to cppreference:

Provides the member typedef type, which is a PODType suitable for use as uninitialized storage for any object whose size is at most Len and whose alignment requirement is a divisor of Align.

The default value of Align is the most stringent (the largest) alignment requirement for any object whose size is at most Len.

In particular, with the default value of Align, the suitably aligned for any object whose size is at most Len.

Note that there are no caveats or exceptions for over-aligned types (and in any case the platform I'm using, gcc, supports at least some over-aligned types).

How can such an implementation actually work? To satisfy the requirement of "any object" it would seem that it would either:

  1. On a platform where alignof(T) <= sizeof(T) for types T, need to always align to roughly Len bytes, since an object of size Len could have an alignment of up to Len. Of course, this would waste a lot of memory for large Len!
  2. On a platform where alignof(T) may be larger than than sizeof(T), I don't see how it could be implemented at all. However, it isn't clear to me that such a type can even exist.

Based on my testing, for default Align values, gcc simply always aligns to 16, regardless of len. This means the storage is not suitable for any object, but only objects of fundamental alignment (alignof(max_align_t) == 16 on this platform).

1 Answer 1

2

Per [meta.trans.other]/2,

It is implementation-defined whether any extended alignment is supported.

It's pretty annoying when they specify stuff like this after the big table without a pointer. I've just added the sentence to cppreference.

Sign up to request clarification or add additional context in comments.

1 Comment

So the footnote "2" doesn't occur anywhere in the text right? What is weird though is that GCC does generally support extended alignments up to at least 128, but not here. I guess there is no constraints that extended alignment support has be be consistent in any implementation, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.