unordered_map插入不使用移动操作

时间:2017-09-21 10:22:00

标签: c++11 g++

我正在尝试将元素插入到unordered_map,它应该使用移动操作,但不使用。

例如:

#include <unordered_map>
#include <memory>
#include <utility>

struct S {
        int i;
};

int main(){
        std::unordered_map<int, std::unique_ptr<S>> map {};

        std::unique_ptr<S> s {new S{1}};

        map.insert({1, std::move(s)});

        return 0;
}

将其编译为:g++-6 -std=c++11 -c Ex2.cpp

获取错误:

In file included from /usr/include/x86_64-linux-gnu/c++/6/bits/c++allocator.h:33:0,
                 from /usr/include/c++/6/bits/allocator.h:46,
                 from /usr/include/c++/6/string:41,
                 from /usr/include/c++/6/stdexcept:39,
                 from /usr/include/c++/6/array:39,
                 from /usr/include/c++/6/tuple:39,
                 from /usr/include/c++/6/unordered_map:41,
                 from Ex2.cpp:1:
/usr/include/c++/6/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, std::unique_ptr<S> >; _Args = {const std::pair<const int, std::unique_ptr<S, std::default_delete<S> > >&}; _Tp = std::pair<const int, std::unique_ptr<S> >]’:
/usr/include/c++/6/bits/alloc_traits.h:455:4:   required from ‘static void std::allocator_traits<std::allocator<_CharT> >::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const int, std::unique_ptr<S> >; _Args = {const std::pair<const int, std::unique_ptr<S, std::default_delete<S> > >&}; _Tp = std::pair<const int, std::unique_ptr<S> >; std::allocator_traits<std::allocator<_CharT> >::allocator_type = std::allocator<std::pair<const int, std::unique_ptr<S> > >]’
/usr/include/c++/6/bits/hashtable_policy.h:1953:37:   required from ‘std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::pair<const int, std::unique_ptr<S, std::default_delete<S> > >&}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false> >; std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false>]’
/usr/include/c++/6/bits/hashtable_policy.h:180:58:   required from ‘std::__detail::_AllocNode<_NodeAlloc>::__node_type* std::__detail::_AllocNode<_NodeAlloc>::operator()(_Arg&&) const [with _Arg = const std::pair<const int, std::unique_ptr<S> >&; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false> >; std::__detail::_AllocNode<_NodeAlloc>::__node_type = std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false>]’
/usr/include/c++/6/bits/hashtable.h:1690:18:   required from ‘std::pair<typename std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, _Traits>::iterator, bool> std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_M_insert(_Arg&&, const _NodeGenerator&, std::true_type) [with _Arg = const std::pair<const int, std::unique_ptr<S> >&; _NodeGenerator = std::__detail::_AllocNode<std::allocator<std::__detail::_Hash_node<std::pair<const int, std::unique_ptr<S> >, false> > >; _Key = int; _Value = std::pair<const int, std::unique_ptr<S> >; _Alloc = std::allocator<std::pair<const int, std::unique_ptr<S> > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<int>; _H1 = std::hash<int>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; typename std::__detail::_Hashtable_base<_Key, _Value, _ExtractKey, _Equal, _H1, _H2, _Hash, _Traits>::iterator = std::__detail::_Node_iterator<std::pair<const int, std::unique_ptr<S> >, false, false>; std::true_type = std::integral_constant<bool, true>]’
/usr/include/c++/6/bits/hashtable_policy.h:713:55:   required from ‘std::__detail::_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::__ireturn_type std::__detail::_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::insert(const value_type&) [with _Key = int; _Value = std::pair<const int, std::unique_ptr<S> >; _Alloc = std::allocator<std::pair<const int, std::unique_ptr<S> > >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<int>; _H1 = std::hash<int>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<false, false, true>; std::__detail::_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::__ireturn_type = std::pair<std::__detail::_Node_iterator<std::pair<const int, std::unique_ptr<S> >, false, false>, bool>; std::__detail::_Insert_base<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::value_type = std::pair<const int, std::unique_ptr<S> >]’
/usr/include/c++/6/bits/unordered_map.h:550:31:   required from ‘std::pair<typename std::_Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc, std::__detail::_Select1st, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Key, _Hash> > >::value, false, true> >::iterator, bool> std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::insert(const value_type&) [with _Key = int; _Tp = std::unique_ptr<S>; _Hash = std::hash<int>; _Pred = std::equal_to<int>; _Alloc = std::allocator<std::pair<const int, std::unique_ptr<S> > >; typename std::_Hashtable<_Key, std::pair<const _Key, _Tp>, _Alloc, std::__detail::_Select1st, _Pred, _Hash, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<std::__not_<std::__and_<std::__is_fast_hash<_Hash>, std::__detail::__is_noexcept_hash<_Key, _Hash> > >::value, false, true> >::iterator = std::__detail::_Node_iterator<std::pair<const int, std::unique_ptr<S> >, false, false>; std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::value_type = std::pair<const int, std::unique_ptr<S> >]’
Ex2.cpp:14:30:   required from here
/usr/include/c++/6/ext/new_allocator.h:120:4: error: use of deleted function ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = std::unique_ptr<S>]’
  { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/6/utility:70:0,
                 from /usr/include/c++/6/unordered_map:38,
                 from Ex2.cpp:1:
/usr/include/c++/6/bits/stl_pair.h:224:17: note: ‘constexpr std::pair<_T1, _T2>::pair(const std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = std::unique_ptr<S>]’ is implicitly deleted because the default definition would be ill-formed:
       constexpr pair(const pair&) = default;
                 ^~~~
/usr/include/c++/6/bits/stl_pair.h:224:17: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = S; _Dp = std::default_delete<S>]’
In file included from /usr/include/c++/6/memory:81:0,
                 from Ex2.cpp:2:
/usr/include/c++/6/bits/unique_ptr.h:356:7: note: declared here
       unique_ptr(const unique_ptr&) = delete;
       ^~~~~~~~~~

所以,错误似乎告诉我我正在使用std :: pair的删除拷贝构造函数,因为std :: unique_ptr没有拷贝构造函数,所以它被删除了。但为什么它不使用移动构造函数?

2017年我的视觉工作室编辑得很好。

我正在使用g ++编译器版本:gcc版本6.2.0 20160901

0 个答案:

没有答案