C++11 未使用移动操作的无序映射插入

C++11 未使用移动操作的无序映射插入,c++11,g++,C++11,G++,我试图将元素插入无序的_映射,它应该使用移动操作,但不使用 例如: #include <unordered_map> #include <memory> #include <utility> struct S { int i; }; int main(){ std::unordered_map<int, std::unique_ptr<S>> map {}; std::unique_

我试图将元素插入无序的_映射,它应该使用移动操作,但不使用

例如:

#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;
}
所以,错误似乎告诉我我正在使用std::pair的已删除副本构造函数,这是因为std::unique\u ptr没有副本构造函数而被删除的。但为什么不使用移动构造函数呢

它在我的visual studio 2017中编译得很好


我使用的是g++编译器版本:gcc版本6.2.0 20160901

您可能需要-std=c++17才能工作。如果不能,请尝试map.emplace(1,std::move(s))map.insert(std::pair{1,std::move(s)})它的编译,如果我使用
map.insert(std::make_pair(1,std::move(s)),它也可以工作下面是发生的情况。C++11提供
templatestd::pair insert(P&&value)过载。这个函数适用于
insert(std::pair…
insert(make_pair…
),但不能用于
insert({…})
,因为模板参数不能从大括号初始化列表中推导出来。C++17进一步提供了
std::pair insert(value\u type&&value)重载-这应该适用于
insert({…})
form。您可能需要-std=c++17才能工作。如果不能,请尝试map.emplace(1,std::move(s))map.insert(std::pair{1,std::move(s)})
它的编译,如果我使用
map.insert(std::make_pair(1,std::move(s)),它也可以工作下面是发生的情况。C++11提供
templatestd::pair insert(P&&value)过载。这个函数适用于
insert(std::pair…
insert(make_pair…
),但不能用于
insert({…})
,因为模板参数不能从大括号初始化列表中推导出来。C++17进一步提供了
std::pair insert(value\u type&&value)重载-这应该适用于
insert({…})
form。
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;
       ^~~~~~~~~~