【发布时间】:2019-01-24 00:35:33
【问题描述】:
如果*map_it 将& 返回到std::pair,这怎么行?
Visual Studio 将我指向pair& operator=(const volatile pair&) = delete;。这是如何工作的?这里发生了什么样的超载魔法?
在尝试实现哈希表时发现它。我希望键是常量,值是可变的,整个对是不可分配的。看了一下Visual Studio 的实现,我还是想不通。我相信这一定是一个我仍然不知道的非常简单的规则。
这是一个例子:
#include <unordered_map>
int main() {
std::unordered_map<int, int> map;
map[1] = 2;
auto map_it = map.begin();
auto & map_pair = *map_it;
// How can they prevent this?
map_pair = std::pair<int const, int>(1, 2);
// And this one?
std::pair<int const, int> p(1, 2);
map_pair = p;
return 0;
}
【问题讨论】:
标签: c++ unordered-map assignment-operator