声明指向过度对齐类型

时间:2016-06-12 02:09:27

标签: c++ c++11 memory-alignment

声明指向对齐类型的指针的语法是什么?指向对齐的对象数组的指针和指向对象数组的指针之间是否存在歧义,每个对象都是对齐的?

例如,如果没有foousing,我将如何实施typedef

template <class T, int N>
using Aligned alignas(N) = T;

void foo(Aligned<float, 64>* vals, int size)
{
    for (int i = 0; i != size; ++i)
        vals[i] *= 2.0f;
}

// the above seems a bit ambiguous -- maybe it could be a pointer to
// floats that are each 64 byte aligned? -- but it gives the same
// code as the following so it seems to be doing the right thing
void bar(float* vals, int size)
{
    vals = (float*)__builtin_assume_aligned(vals, 64);
    for (int i = 0; i != size; ++i)
        vals[i] *= 2.0f;
}

0 个答案:

没有答案