少于模板参数的比较

时间:2015-12-06 02:37:30

标签: c++ templates

我有一个模板化的类,它实现了一种类似于(伪)地图的红黑树。伪映射,因为它不会存储键,只存储值,键中嵌入了键。它需要operator ==和operator<重载以针对任意键测试存储的值并相互反对(使用嵌入的键)。 E.g。

struct Val
{
    //some actual data
    std::string key;

    bool operator==(const Val &val) { return this->key == val.key; }
    bool opeartor==(const std::string &str) { return this->key == str; }
    bool opeartor<(const Val &val) { return this->key < val.key; }
    bool opeartor<(const std::string &str) { return this->key < str; }
};

这种&#34;价值&#34;会进入这个模板化的课程:

template<typename T>
class Map
{
    struct Node
    {
        //...
        T data;
    };
    public:
        //..
        template<typename K> T value(const K &key) const
        {
            Node *it;
            //...
            if(it->data == key)
            //...

            int dir = (it->data < key);
            //...
        }
};

然而,当行if(it->data == key)检出正常(我还没有使用该类)时,第二个int dir = (it->data < key);没有错误&#34;解析模板参数列表中的错误&#34;。奇怪的是,如果我将比较更改为<=,它编译得很好。但是在那时我已经知道它不相等(第一次检查<会这样做。

如何修复它,为什么抱怨一个操作员而不是其他操作员?

1 个答案:

答案 0 :(得分:0)

T.C。已经在评论中发布了答案,所以我将重复它作为答案。这种行为是由于GCC https://gcc.gnu.org/bugzilla/show_bug.cgi?id=10200中长期存在的错误,当一个人在命名方面不是很有创意并且在类中有一堆相同的名称(在这种情况下,成员变量名为data时嵌套类和超类中名为data的成员方法。)