具有多个数组的typedef数组

时间:2016-05-21 17:21:29

标签: c++ c arrays struct typedef

我希望能够做到这一点:

typedef int a[2], b[2], c[2];

无需输入[2]。一种解决方案是:

typedef struct { int a[2] } a, b, c;

但是你必须总是做a.a[0]这样的事情并且没有用。

3 个答案:

答案 0 :(得分:4)

好吧,这个问题用C ++和C标记,但是有一个简单的C ++ 11解决方案:

using int_arr = int[2];
int_arr a, b, c;

答案 1 :(得分:4)

对于C或C ++ 98,一个简单的typedef将执行:

typedef int int2[2];
int2 a, b, c;

答案 2 :(得分:1)

我宁愿定义一个更可重用的类型和SIMD友好,即

template<typename T, int size>
struct type_t {
    // 32-byte AVX aligned ready for [gnu] auto-vectorization 
    typedef T array_t[size] __attribute__((aligned(32)));
};

typedef type_t<int, 2>::array_t int_array2;
typedef type_t<double, 2>::array_t double_array2;

// and then
int_array2 a, b, c;
double_array2 d, e, f;