std :: aligned_storage如何为任何对象公开正确对齐的存储? [英] How can std::aligned_storage expose correctly aligned storage for any object?

查看:76
本文介绍了std :: aligned_storage如何为任何对象公开正确对齐的存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std::aligned_storage结构提供了至少根据 cppreference的type typedef :

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

提供成员typedef类型,它是适合使用的PODType 对于任何大小最大为Len和的对象,作为未初始化的存储 其对齐要求是Align的除数.

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.

Align的默认值是最严格的(最大) 任何尺寸最大为Len的物体的对齐要求.

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

尤其是,默认值为Align,适用于大小小于或等于Len的任何对象.

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

请注意,对于过度对齐的类型(在我使用的平台gcc,至少支持某些过度对齐的类型)没有任何警告或例外.

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. 在类型为Talignof(T) <= sizeof(T)的平台上,必须始终对齐到大致Len个字节,因为大小为Len的对象可能具有最大为Len的对齐方式.当然,这对于大型Len会浪费很多内存!
  2. alignof(T)可能大于sizeof(T)的平台上,我完全不知道如何实现.但是,不清楚我,甚至可以存在这种类型.
  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.

基于我的测试,用于默认Align值,gcc始终总是与16对齐,而与len无关.这意味着存储不适合任何对象,而仅适用于基本对齐的对象(在此平台上为alignof(max_align_t) == 16).

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).

推荐答案

每个

由实现方式定义是否支持任何扩展的对齐方式.

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

当他们在没有指针的大表之后指定这样的内容时,这很烦人.我刚刚将句子添加到cppreference中.

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.

这篇关于std :: aligned_storage如何为任何对象公开正确对齐的存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆