<html>
<head>
<base href="https://llvm.org/bugs/" />
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW --- - Erase a key which is not exists in unordered_map(in c++11) can compile will."
href="https://llvm.org/bugs/show_bug.cgi?id=24403">24403</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Erase a key which is not exists in unordered_map(in c++11) can compile will.
</td>
</tr>
<tr>
<th>Product</th>
<td>clang
</td>
</tr>
<tr>
<th>Version</th>
<td>3.6
</td>
</tr>
<tr>
<th>Hardware</th>
<td>PC
</td>
</tr>
<tr>
<th>OS</th>
<td>Linux
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>normal
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>C++11
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>qinjunzhong@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>dgregor@apple.com, llvm-bugs@lists.llvm.org
</td>
</tr>
<tr>
<th>Classification</th>
<td>Unclassified
</td>
</tr></table>
<p>
<div>
<pre>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</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>