完整模板专业化错误
Full Template Specialization errors
因为我正在对我的代码进行 SIMD 化处理,所以我决定使用模板专门化来处理每种类型的情况,但似乎我做错了什么。
template<typename T> struct __declspec(align(16)) TVec2
{
};
template<> __declspec(align(16)) struct TVec2<s64>
{
union
{
struct
{
s64 x, y;
};
struct
{
__m128i v;
};
};
TVec2()
{
v = _mm_setzero_si128();
}
TVec2(s64 scalar)
{
v = _mm_set_epi64x(scalar, scalar);
}
TVec2(s64 x, s64 y)
{
v = _mm_set_epi64x(x, y);
}
template<typename U> operator TVec2<U>() const
{
return TVec2<U>(static_cast<U>(x), static_cast<U>(y));
}
s64& operator[](word index)
{
return v.m128i_i64[index];
}
const s64& operator[](word index) const
{
return v.m128i_i64[index];
}
};
// There are other specializations but they produce the same errors
当我在 Visual Studio (2015) 中编译时,我得到 (C2988: unrecognizeable template declaration/definition) 后跟 (C2059: syntax error: "< end Parse >")。我相当确定我正确地遵循了专业化文档,但我很容易出错。
看来问题是结构关键字前写了__declspec导致模板无法正确识别。尝试更改为
template<> struct __declspec(align(16)) TVec2<s64>
使用 alignas specifier 并摆脱无名 struct/union 也是一个好主意。
因为我正在对我的代码进行 SIMD 化处理,所以我决定使用模板专门化来处理每种类型的情况,但似乎我做错了什么。
template<typename T> struct __declspec(align(16)) TVec2
{
};
template<> __declspec(align(16)) struct TVec2<s64>
{
union
{
struct
{
s64 x, y;
};
struct
{
__m128i v;
};
};
TVec2()
{
v = _mm_setzero_si128();
}
TVec2(s64 scalar)
{
v = _mm_set_epi64x(scalar, scalar);
}
TVec2(s64 x, s64 y)
{
v = _mm_set_epi64x(x, y);
}
template<typename U> operator TVec2<U>() const
{
return TVec2<U>(static_cast<U>(x), static_cast<U>(y));
}
s64& operator[](word index)
{
return v.m128i_i64[index];
}
const s64& operator[](word index) const
{
return v.m128i_i64[index];
}
};
// There are other specializations but they produce the same errors
当我在 Visual Studio (2015) 中编译时,我得到 (C2988: unrecognizeable template declaration/definition) 后跟 (C2059: syntax error: "< end Parse >")。我相当确定我正确地遵循了专业化文档,但我很容易出错。
看来问题是结构关键字前写了__declspec导致模板无法正确识别。尝试更改为
template<> struct __declspec(align(16)) TVec2<s64>
使用 alignas specifier 并摆脱无名 struct/union 也是一个好主意。