2

Can somebody please help me to port the following code to GCC? 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 error: explicit specialization in non-namespace scope 'class Data_T', which was caused by specialization function IsEqual(), but now encountered another type of error (no matching function for call to 'Data_T::IsEqual(float, const float&)'), which seems to be caused by the __vector_size__ attribute, which I can't remove. Please help...

Ryan
  • 1,451
  • 2
  • 27
  • 36

1 Answers1

3

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);
    }
};

Edit regarding update:
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.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
  • @Ryan: Not really, just had my fair share of problems already :) – Georg Fritzsche Apr 20 '11 at 07:35
  • :) Actually, I'm sorry to say, but this solution doesn't really work for me... IsEqual() function calls give me an error "No matching function call to..." if I pass MyData arguments, which are actually based on float type... Back to square 1 :( – Ryan Apr 20 '11 at 08:14
  • @Ryan: I can't see why, can you edit an example into the question that reproduces the problem? – Georg Fritzsche Apr 20 '11 at 08:30
  • @Ryan: That edit should catch it, totally overlooked that typo. By the way, at least for me on Apples GCC 4.2 there is no conversion from `float` to `float __vector__`. – Georg Fritzsche Apr 20 '11 at 10:57