Reputation: 101
I am trying to figure out if any type/class/struct/etc in C++ standard library (for example std::vector or std::thread) in theory could be over-aligned (alignof(T) > alignof(max_align_t)). It is not clear for me from specification. Is it required that every type from standard library have fundamental alignment requirement?
Upvotes: 0
Views: 241
Reputation: 238441
Is it required that every type from standard library have fundamental alignment requirement?
There is no such requirement.
Can types in standard library be over-aligned?
Yes. Here is an example that passes on a particular system for example. It demonstrates an overaligned type that is instance of a standard library template:
constexpr std::size_t overaligned = alignof(std::max_align_t) * 2;
struct alignas(overaligned) test {};
static_assert(alignof(std::array<test, 1>) > alignof(std::max_align_t));
If type is over-aligned than I can't use plain malloc/new for it.
You can use new
on over aligned types since C++17 if extended alignment is supported in the first place. In C++11, whether over aligned new is supported is implementation defined even if the implementation does support over aligned types.
Upvotes: 2