[英]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.