
Question:
Can somebody please help me to port the following code to <strong>GCC</strong>? I have found a lot or related questions on this site, but I just can't seem to apply suggested workarounds in my case...
typedef float MyData __attribute__ ((__vector_size__ (16)));
template <typename T> class Data_T
{
public:
template < typename U > static bool IsEqual (const T & a, const T & b)
{
return a == b;
}
//Fixed: template <> static bool IsEqual < MyData > ( const MyData & a, const MyData & b)
static bool IsEqual ( const MyData & a, const MyData & b)
{
return true;
}
void TriggerProblem(const T & val)
{
if (!IsEqual(1.0f, val)) // Error: no matching function for call to 'Data_T<float>::IsEqual(float, const float&)'
{
}
}
};
The code to trigger the problem:
Data_T<MyData> inst;
inst.TriggerProblem(1.0f);
I was getting an error <strong>error: explicit specialization in non-namespace scope 'class Data_T'</strong>, which was caused by specialization function <strong>IsEqual()</strong>, but now encountered another type of error (<strong>no matching function for call to 'Data_T::IsEqual(float, const float&)'</strong>), which seems to be caused by the <strong>__vector_size__</strong> attribute, which I can't remove. Please help...
Answer1:In this case overloading should suffice instead of specializing:
template <typename T> class Data_T
{
public:
template<typename U> static bool IsEqual(const U& a, const U& b)
{
return a == b;
}
static bool IsEqual(const MyData& a, const MyData& b)
{
return MyDataTypeIsEqual (a, b);
}
template<typename U> bool IsRangeEqual(const U& a, const U& b, const U& delta)
{
return (a >= b) ? (a - b <= delta) : (b - a <= delta);
}
bool IsRangeEqual(const MyData & a, const MyData & b, const MyData & accuracy)
{
return MyDataTypeIsRangeEqual (a, b, accuracy);
}
};
<em>Edit regarding update</em>:<br />
While for me the conversion from float
to float __vector__
already fails, you seem to have a typo of:
template<typename U> static bool IsEqual(const T& a, const T& b)
vs.:
template<typename U> static bool IsEqual(const U& a, const U& b)
I fixed the above code accordingly.