确定C ++中的最大可能对齐 [英] Determining maximum possible alignment in C++

查看:117
本文介绍了确定C ++中的最大可能对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何便携式方式来确定任何类型的最大可能对齐方式是什么?

Is there any portable way to determine what the maximum possible alignment for any type is?

在x86上的示例,SSE指令需要16字节对齐,但据我所知,没有指令需要更多,所以任何类型都可以安全地存储到16字节对齐缓冲区。

For example on x86, SSE instructions require 16-byte alignment, but as far as I'm aware, no instructions require more than that, so any type can be safely stored into a 16-byte aligned buffer.

我需要创建一个缓冲区(例如一个char数组),我可以写任意类型的对象,所以我需要能够依赖于开始

I need to create a buffer (such as a char array) where I can write objects of arbitrary types, and so I need to be able to rely on the beginning of the buffer to be aligned.

如果所有其他都失败,我知道分配一个具有 new 的字符数组是有保证的具有最大对齐,但是使用TR1 / C ++ 0x模板 alignment_of aligned_storage ,我想知道是否可以在我的缓冲区类中创建缓冲区,而不需要动态分配的数组的额外指针间接。

If all else fails, I know that allocating a char array with new is guaranteed to have maximum alignment, but with the TR1/C++0x templates alignment_of and aligned_storage, I am wondering if it would be possible to create the buffer in-place in my buffer class, rather than requiring the extra pointer indirection of a dynamically allocated array.

想法?

我知道有很多选项用于确定有限类型集合的最大对齐:A union,或者只是来自TR1的 alignment_of ,但我的问题是类型集是无限的。我不知道哪些对象必须存储到缓冲区中。

I realize there are plenty of options for determining the max alignment for a bounded set of types: A union, or just alignment_of from TR1, but my problem is that the set of types is unbounded. I don't know in advance which objects must be stored into the buffer.

推荐答案

在C ++ 0x中, Align 模板参数 std :: aligned_storage< Len,Align> 的默认参数为default-alignment定义为(N3225§20.7.6.6表56):

In C++0x, the Align template parameter of std::aligned_storage<Len, Align> has a default argument of "default-alignment," which is defined as (N3225 §20.7.6.6 Table 56):


默认对齐的值应是任何C ++对象类型大小不大于 Len

清除是否将SSE类型视为C ++对象类型。

It isn't clear whether SSE types would be considered "C++ object types."

默认参数不是TR1的一部分 aligned_storage ;它被添加为C ++ 0x。

The default argument wasn't part of the TR1 aligned_storage; it was added for C++0x.

这篇关于确定C ++中的最大可能对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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