gpt4 book ai didi

c++ - 标准库中的类型可以过度对齐吗?

转载 作者:行者123 更新时间:2023-11-28 04:09:19 24 4
gpt4 key购买 nike

我想弄清楚 C++ 标准库中的任何类型/类/结构/等(例如 std::vector 或 std::thread)在理论上是否可以过度对齐(alignof(T) > alignof( max_align_t)).从规范上我不清楚。是否要求标准库中的每个类型都具有基本对齐要求?

最佳答案

Is it required that every type from standard library have fundamental alignment requirement?

没有这样的要求。

Can types in standard library be over-aligned?

是的。例如,这是一个传递特定系统的示例。它演示了一个作为标准库模板实例的过度对齐类型:

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.

如果首先支持扩展对齐,则可以在自 C++17 起的过对齐类型上使用 new。在 C++11 中,是否支持过度对齐的 new 是实现定义的,即使实现确实支持过度对齐的类型也是如此。

关于c++ - 标准库中的类型可以过度对齐吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58151583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com