【发布时间】:2013-09-09 09:37:53
【问题描述】:
我正在尝试使用 std::aligned_storage 模式实现简单静态数组的 16 字节对齐:
#include <type_traits>
int main()
{
const size_t SIZE = 8;
using float_16 = std::aligned_storage<sizeof(float) * SIZE, 16>::type;
float_16 mas;
new(&mas) float[SIZE];//Placement new. Is this necessary?
mas[0]=1.f;//Compile error while attempting to set elements of aligned array
}
我得到以下编译错误:
在«mas[0]»中没有匹配«operator[]»
然后我尝试使用显式指针转换:
float* mas_ = reinterpret_cast<float*>(mas);
但这也会产生编译错误:
类型 «float_16 {aka std::aligned_storage::type}» 输入 «float*»
谁能建议我如何正确使用 std::aligned_storage 对齐静态数组?
【问题讨论】:
-
为什么不用
new返回的指针? -
new返回的@avakar 指针没有扩展对齐。 -
@R.MartinhoFernandes:我认为 avakar 谈论的是新的展示位置(对齐)。 (见我的回答)
-
@R.MartinhoFernandes,Jarod42 是正确的,我自然是在谈论原始问题中的新位置。
标签: c++ arrays pointers c++11 memory-alignment