为什么有序的std::map和有序的std::set没有被邀请进行transaction_safe

Why are ordered std::map and ordered std::set not invited to make transaction_safe?

本文关键字:std transaction safe set map 为什么      更新时间:2023-10-16

众所周知,实验C++中有事务内存TS(ISO/IEC TS 19841:2015):将函数声明为transaction_safe就足够了吗?

而声明为transaction_safeoperator[]仅适用于容器:std::vectorstd::unordered_mapstd::unordered_multimapstd::unordered_setstd::unordered_multisetstd::deque-取自n4514:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4514.pdf

23.4关联容器[关联]

23.4.4类模板图【图】

23.4.4.1类别模板地图概述[map.overview]

在23.4.4.1[map.overview]中,将"transaction_safe"添加到声明中开始和结束成员函数的所有变体以及size、max_size和empty的声明。

但是为什么对于std::mapstd::set没有operator[]声明为transaction_safe(但是对于unordered_map/unordered_set有)?

为什么要在std::mapstd::setbeginend成员函数的所有变体的声明中添加"transaction_safe"?

迭代器beginend对于std::arraystd::vectorstd::list是非常必要的,但对于关联阵列则不是。在关联数组中,需要查找或查找并修改函数:findatinserteraseoperator[]。没有他们,这是没有意义的。

为什么有序的std::map和有序的std::set没有被邀请进行transaction_safe?

unordered_meow::operator[]被指定为无条件事务安全是一个缺陷。

  • 首先,unordered_setunordered_multisetunordered_multimap根本没有operator[]
  • unordered_map::operator[]必须调用HashPred,并可能分配内存和构造新的键值对;这些都不一定是交易安全的

相反,map::operator[]的事务安全性是由添加到[container.requirements.general]:来管理的

除非无条件指定为事务安全,否则中的函数如果所有必需的操作都是交易安全。[注意:这包括对元素的操作类型,在std::allocator_traitsComparePredHash对象上,取决于各自的功能尾注]