簡體   English   中英

少於模板參數的比較

[英]Less than comparison of template arguments

我有一個實現紅黑樹的模板化類,其行為類似於(偽)映射。 偽映射,因為它將不存儲鍵,而僅存儲值,並且鍵被嵌入值中。 這將需要operator ==和operator <重載以針對任意鍵測試存儲的值,然后再次相互重疊(使用嵌入式鍵)。 例如

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

這種“值”將進入此模板化類:

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); 沒有錯誤“模板參數列表中的解析錯誤”。 奇怪的是,如果我將比較值更改為<=則可以正常編譯。 但是在那一點上,我已經知道這是不相等的(第一個檢查是<這樣。

我該如何解決它,為什么它抱怨一個操作員而不是其他操作員?

TC已在評論中發布了答案,因此我將其重復作為答案。 此行為是由於GCC https://gcc.gnu.org/bugzilla/show_bug.cgi?id=10200中存在的長期錯誤所致,當一個人的命名不是很有創意並且在名稱中具有一堆相同的名稱時,該錯誤就會顯現出來。類(在這種情況下,成員變量在嵌套類中名為data ,而成員方法在超類中稱為data )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM