【问题标题】:Explicit specialization in non-namespace scope error... Desperately need help非命名空间范围错误中的显式专业化......迫切需要帮助
【发布时间】:2011-04-19 10:01:07
【问题描述】:

有人可以帮我将以下代码移植到 GCC 吗?我在这个网站上发现了很多或相关的问题,但我似乎无法在我的情况下应用建议的解决方法......

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&)'
        {

        }
    }
};

触发问题的代码:

Data_T<MyData> inst;
inst.TriggerProblem(1.0f);

我收到一个错误error: explicit specialization in non-namespace scope 'class Data_T',这是由专业化函数IsEqual()引起的,但现在遇到了另一个错误类型(no matching function for call to 'Data_T::IsEqual(float, const float&)'),这似乎是由 __vector_size__ 属性引起的,我无法删除。请帮忙...

【问题讨论】:

  • C99和它有什么关系,这是C++代码?
  • 对不起,我的意思是 ISO/IEC 14882:2003 标准

标签: c++ templates gcc


【解决方案1】:

在这种情况下,重载应该足够了,而不是专门化:

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

关于更新的编辑
虽然对我来说从floatfloat __vector__ 的转换已经失败,但您似乎有一个错字:

template<typename U> static bool IsEqual(const T& a, const T& b)

对比:

template<typename U> static bool IsEqual(const U& a, const U& b)

我相应地修复了上面的代码。

【讨论】:

  • @Ryan:不是真的,只是我已经遇到了很多问题:)
  • :) 实际上,我很抱歉,但这个解决方案对我来说真的不起作用... IsEqual() 函数调用给我一个错误“没有匹配的函数调用... " 如果我​​传递 MyData 参数,这些参数实际上是基于浮点类型的......回到方块 1 :(
  • @Ryan:我不明白为什么,你能在重现问题的问题中编辑一个示例吗?
  • @Ryan:那个编辑应该抓住它,完全忽略了那个错字。顺便说一句,至少对我来说,在 Apples GCC 4.2 上没有从 floatfloat __vector__ 的转换。
猜你喜欢
  • 2011-03-04
  • 2011-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-07
相关资源
最近更新 更多