Ryan
Ryan

Reputation: 1501

Explicit specialization in non-namespace scope error... Desperately need help

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...

Upvotes: 2

Views: 2549

Answers (1)

Georg Fritzsche
Georg Fritzsche

Reputation: 98984

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.

Upvotes: 3

Related Questions