gpt4 book ai didi

c++ - 完整模板特化错误

转载 作者:行者123 更新时间:2023-11-28 05:13:06 25 4
gpt4 key购买 nike

因为我正在对我的代码进行 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: 无法识别的模板声明/定义) 后跟 (C2059: 语法错误: "< end Parse >")。我相当确定我正确地遵循了特化文档,但我很容易出错。

最佳答案

看起来问题是由于__declspec 写在structure 关键字之前导致无法正确识别模板。尝试更改为

template<> struct __declspec(align(16)) TVec2<s64>

使用 alignas specifier 可能也是个好主意并摆脱无名的结构/union 。

关于c++ - 完整模板特化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43165404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com