简体   繁体   English

少于模板参数的比较

[英]Less than comparison of template arguments

I have a templated class implementing red black tree that would behave kind of like a (pseudo)map. 我有一个实现红黑树的模板化类,其行为类似于(伪)映射。 Pseudo-map because it would not store keys, only values, with keys being embedded in the values. 伪映射,因为它将不存储键,而仅存储值,并且键被嵌入值中。 It would require operator== and operator< overloads to test stored values against an arbitrary key and againts each other (using embedded key). 这将需要operator ==和operator <重载以针对任意键测试存储的值,然后再次相互重叠(使用嵌入式键)。 Eg 例如

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

This kind of "value" would go into this templated class: 这种“值”将进入此模板化类:

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

However while the line if(it->data == key) checks out fine (I am not using the class yet) the second one int dir = (it->data < key); 但是,在if(it->data == key)检查正常(我还没有使用该类)的同时,第二个int dir = (it->data < key); does not with error "parse error in template argument list". 没有错误“模板参数列表中的解析错误”。 Strangely if I change the comparison to <= it compiles fine. 奇怪的是,如果我将比较值更改为<=则可以正常编译。 However at that point I already know it is not equal (the first check so < would do. 但是在那一点上,我已经知道这是不相等的(第一个检查是<这样。

How do I fix it and why is it complaining about one operator and not the others? 我该如何解决它,为什么它抱怨一个操作员而不是其他操作员?

TC has posted the answer in the comments so I will just repeat it as an answer. TC已在评论中发布了答案,因此我将其重复作为答案。 The behaviour is due to long-standing bug in GCC https://gcc.gnu.org/bugzilla/show_bug.cgi?id=10200 that manifests itself when one is not very creative with naming and has bunch of the same names in the class (in this case member variable named data in the nested class and the member method called data in the super-class). 此行为是由于GCC https://gcc.gnu.org/bugzilla/show_bug.cgi?id=10200中存在的长期错误所致,当一个人的命名不是很有创意并且在名称中具有一堆相同的名称时,该错误就会显现出来。类(在这种情况下,成员变量在嵌套类中名为data ,而成员方法在超类中称为data )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在模板元编程中进行比较? - How to make a less than comparison in template meta-programming? 与将条件作为模板参数传递给sort()的比较导致比将条件函数指针传递给qsort()更少的开销? - Comparison with passing criteria as template parameter to sort() results in less overhead than passing criteria function pointer to qsort()? 比起比较或小于或等于是否更有效? - Is It More Efficient to Do a Less Than comparison OR a Less Than Or Equal To? “不到”ifstream与GCC 4对6的比较 - “less than” Comparison on ifstream with GCC 4 vs. 6 对字符串使用小于比较运算符 - Using the less than comparison operator for strings 无论情况如何,字符串比较的Less Than运算符都会产生相同的结果 - Less Than operator on string comparison yields same result no matter the situation 如何在双精度数组上定义一个比较运算符(小于)? - how to define a comparison operator (less than) on array of doubles? 使用迭代器之间的比较来遍历std :: map - traverse the std::map with less than comparison between iterators 为什么比较40小于等于-2147483648为真? - Why comparison of 40 is less than or equal to -2147483648 true? 为什么带有默认模板 arguments 的模板不能用作模板模板参数中具有较少模板参数的模板 - Why the template with default template arguments can't be used as template with less template argument in Template Template Parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM