[llvm-bugs] [Bug 24403] New: Erase a key which is not exists in unordered_map(in c++11) can compile will.

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Aug 8 18:03:08 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=24403

            Bug ID: 24403
           Summary: Erase a key which is not exists in unordered_map(in
                    c++11) can compile will.
           Product: clang
           Version: 3.6
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++11
          Assignee: unassignedclangbugs at nondot.org
          Reporter: qinjunzhong at gmail.com
                CC: dgregor at apple.com, llvm-bugs at lists.llvm.org
    Classification: Unclassified

As summary tails, but the code will compile failed by using virtual studio 2015
community. My code is as follow:
#include <iostream>
#include <string>
#include <unordered_map>
#include <list>
using namespace std;


class LRUCache {
    struct CacheNode {
        int key;
        int value;
        CacheNode(int k, int v): key(k), value(v) { }
    };
public:
    LRUCache(int _capacity) {
        this->capacity = _capacity;
    }
    int get(int key) {
        if (cacheMap.find(key) == cacheMap.end()) return -1;
        cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
        cacheMap[key] = cacheList.begin();
        return cacheMap[key]->value;
    }
    int size() {
        return capacity;
    }
    void set(int key, int value) {
        if (cacheMap.find(key) != cacheMap.end()) {
            cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
            cacheMap[key] = cacheList.begin();
            cacheMap[key]->value = value;
            return;
        }
        if (cacheList.size() == capacity) {
            cout << "key = " << key << endl;
            cacheMap.erase(key); // where the different result by using vs2015
and clang++
            cacheList.pop_back();
        }
        cacheList.push_front(CacheNode(key, value));
        cacheMap[key] = cacheList.begin();
    }
private:
    int capacity;
    list<CacheNode> cacheList;
    unordered_map<int, list<CacheNode>::iterator> cacheMap;
};

int main() {
    LRUCache lru = LRUCache(2);
    lru.set(2, 1);
    lru.set(1, 1);
    lru.set(2, 3);
    lru.set(4, 1);
    cout << lru.get(1) << endl;
    cout << lru.get(2) << endl;
    return 0;
}

I don't if it is a bug. But it can really compiled by clang++ and it can run
out the result, the result is:
key = 4
1
3

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150809/15b1c2d8/attachment-0001.html>


More information about the llvm-bugs mailing list