gpt4 book ai didi

c++ - 具有不可 move 的默认可构造值类型的 map/unordered_map

转载 作者:太空狗 更新时间:2023-10-29 21:00:22 24 4
gpt4 key购买 nike

更新:出现在23.5.4.3 herem[1]版本应该是有效的

更新 2:m[1]正在使用 gcc4.9.1

具体来说,std::mutex .假设我想要一个 std::unordered_map<int, std::mutex> .这可能吗?

我似乎无法添加 mutexmap在某种程度上不会在已删除的复制构造函数上触发错误,mutex 中的任何一个或实习生std::pair持有互斥量。

m.emplace(1);
m.emplace(1, {});
m[1];

全部编译失败。我真正想要的是 mutexmap 内就地 build .

我不想使用 std::unique_ptr<std::mutex>但我知道这可能是我的最佳选择。

添加:operator[] 的完整示例在 gcc 和 clang 下这对我来说失败了

#include <unordered_map>
#include <mutex>

int main() {
std::unordered_map<int, std::mutex> m;
m[1];

return 0;
}

来自 clang3.5 的错误中最相关的部分

/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/bits/stl_pair.h:126:35: error: call to deleted constructor of 'std::mutex'
: first(std::forward<_U1>(__x)), second(__y) { }
^ ~~~
...

f.cpp:6:6: note: in instantiation of member function 'std::__detail::_Map_base<...>::operator[]' requested here
m[1];
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/../../../../include/c++/4.7/mutex:163:5: note: 'mutex' has been explicitly marked deleted here
mutex(const mutex&) = delete;
^

来自 gcc 4.7.2

In file included from /usr/include/c++/4.7/utility:72:0,
from /usr/include/c++/4.7/unordered_map:38,
from f.cpp:1:
/usr/include/c++/4.7/bits/stl_pair.h: In instantiation of ‘constexpr std::pair<_T1, _T2>::pair(_U1&&, const _T2&) [with _U1 = int; <template-parameter-2-2> = void; _T1 = int; _T2 = std::mutex]’:
/usr/include/c++/4.7/bits/stl_pair.h:273:72: required from ‘constexpr std::pair<typename std::__decay_and_strip<_T1>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = int; _T2 = std::mutex; typename std::__decay_and_strip<_T2>::__type = std::mutex; typename std::__decay_and_strip<_T1>::__type = int]’
/usr/include/c++/4.7/bits/hashtable_policy.h:463:24: required from ‘std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type& std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::operator[](_Key&&) [with _Key = int; _Pair = std::pair<const int, std::mutex>; _Hashtable = std::_Hashtable<int, std::pair<const int, std::mutex>, std::allocator<std::pair<const int, std::mutex> >, std::_Select1st<std::pair<const int, std::mutex> >, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>; std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type = std::mutex]’
f.cpp:6:8: required from here
/usr/include/c++/4.7/bits/stl_pair.h:126:45: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from f.cpp:2:0:
/usr/include/c++/4.7/mutex:163:5: error: declared here
In file included from /usr/include/c++/4.7/utility:72:0,
from /usr/include/c++/4.7/unordered_map:38,
from f.cpp:1:
/usr/include/c++/4.7/bits/stl_pair.h: In instantiation of ‘constexpr std::pair<typename std::__decay_and_strip<_T1>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) [with _T1 = int; _T2 = std::mutex; typename std::__decay_and_strip<_T2>::__type = std::mutex; typename std::__decay_and_strip<_T1>::__type = int]’:
/usr/include/c++/4.7/bits/hashtable_policy.h:463:24: required from ‘std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type& std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::operator[](_Key&&) [with _Key = int; _Pair = std::pair<const int, std::mutex>; _Hashtable = std::_Hashtable<int, std::pair<const int, std::mutex>, std::allocator<std::pair<const int, std::mutex> >, std::_Select1st<std::pair<const int, std::mutex> >, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, false, false, true>; std::__detail::_Map_base<_Key, _Pair, std::_Select1st<_Pair>, true, _Hashtable>::mapped_type = std::mutex]’
f.cpp:6:8: required from here
/usr/include/c++/4.7/bits/stl_pair.h:273:72: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = int; _T2 = std::mutex; std::pair<_T1, _T2> = std::pair<int, std::mutex>]’
/usr/include/c++/4.7/bits/stl_pair.h:120:17: note: ‘constexpr std::pair<_T1, _T2>::pair(std::pair<_T1, _T2>&&) [with _T1 = int; _T2 = std::mutex; std::pair<_T1, _T2> = std::pair<int, std::mutex>]’ is implicitly deleted because the default definition would be ill-formed:
/usr/include/c++/4.7/bits/stl_pair.h:120:17: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from f.cpp:2:0:
/usr/include/c++/4.7/mutex:163:5: error: declared here

最佳答案

两个选项。

一个是您尝试的第三件事 m[1] 应该可以编译。你遇到了什么错误?

第二种是使用 emplacepiecewise_construct 构造函数:

m.emplace(std::piecewise_construct, std::make_tuple(1), std::tuple<>());

应该可以工作,尽管我认为很可能存在标准库但它没有。

关于c++ - 具有不可 move 的默认可构造值类型的 map/unordered_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22229773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com