在std :: map中使用std :: reference_wrapper

世界末日

我以为地图和reference_wrappers会很容易,但我绊倒了一些奇怪的东西:

#include <map>
#include <functional>

int main(void) {
    std::map<int, std::reference_wrapper<const int>> mb;
    const int a = 5;
    mb[0] = std::cref(a);
}

这段代码为我提供了以下编译器错误:

In file included from c:/MinGW/x86_64-w64-mingw32/include/c++/bits/stl_map.h:63:0,
                 from c:/MinGW/x86_64-w64-mingw32/include/c++/map:61,
                 from ../test/main.cpp:9:
c:/MinGW/x86_64-w64-mingw32/include/c++/tuple: In instantiation of 'std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {int&&}; long long unsigned int ..._Indexes1 = {0ull}; _Args2 = {}; long long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = std::reference_wrapper<const int>]':
c:/MinGW/x86_64-w64-mingw32/include/c++/tuple:1083:63:   required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {int&&}; _Args2 = {}; _T1 = const int; _T2 = std::reference_wrapper<const int>]'
c:/MinGW/x86_64-w64-mingw32/include/c++/ext/new_allocator.h:120:4:   required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, std::reference_wrapper<const int> >; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, std::reference_wrapper<const int> > >]'
c:/MinGW/x86_64-w64-mingw32/include/c++/bits/alloc_traits.h:253:4:   required from 'static std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::pair<const int, std::reference_wrapper<const int> >; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Alloc = std::allocator<std::_Rb_tree_node<std::pair<const int, std::reference_wrapper<const int> > > >; std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> = void]'
c:/MinGW/x86_64-w64-mingw32/include/c++/bits/alloc_traits.h:399:57:   required from 'static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::pair<const int, std::reference_wrapper<const int> >; _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Alloc = std::allocator<std::_Rb_tree_node<std::pair<const int, std::reference_wrapper<const int> > > >; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]'
c:/MinGW/x86_64-w64-mingw32/include/c++/bits/stl_tree.h:423:42:   required from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, std::reference_wrapper<const int> >; _KeyOfValue = std::_Select1st<std::pair<const int, std::reference_wrapper<const int> > >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, std::reference_wrapper<const int> > >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, std::reference_wrapper<const int> > >*]'
c:/MinGW/x86_64-w64-mingw32/include/c++/bits/stl_tree.h:1789:64:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, std::reference_wrapper<const int> >; _KeyOfValue = std::_Select1st<std::pair<const int, std::reference_wrapper<const int> > >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, std::reference_wrapper<const int> > >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const int, std::reference_wrapper<const int> > >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const int, std::reference_wrapper<const int> > >]'
c:/MinGW/x86_64-w64-mingw32/include/c++/bits/stl_map.h:519:8:   required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int; _Tp = std::reference_wrapper<const int>; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, std::reference_wrapper<const int> > >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = std::reference_wrapper<const int>; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]'
../test/main.cpp:20:6:   required from here
c:/MinGW/x86_64-w64-mingw32/include/c++/tuple:1094:70: error: no matching function for call to 'std::reference_wrapper<const int>::reference_wrapper()'
         second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...)
                                                                      ^
c:/MinGW/x86_64-w64-mingw32/include/c++/tuple:1094:70: note: candidates are:
In file included from ../test/main.cpp:10:0:
c:/MinGW/x86_64-w64-mingw32/include/c++/functional:413:7: note: std::reference_wrapper<_Tp>::reference_wrapper(const std::reference_wrapper<_Tp>&) [with _Tp = const int]
       reference_wrapper(const reference_wrapper<_Tp>& __inref) noexcept
       ^
c:/MinGW/x86_64-w64-mingw32/include/c++/functional:413:7: note:   candidate expects 1 argument, 0 provided
c:/MinGW/x86_64-w64-mingw32/include/c++/functional:407:7: note: std::reference_wrapper<_Tp>::reference_wrapper(_Tp&) [with _Tp = const int]
       reference_wrapper(_Tp& __indata) noexcept
       ^
c:/MinGW/x86_64-w64-mingw32/include/c++/functional:407:7: note:   candidate expects 1 argument, 0 provided

我在这里做的事情真的非常愚蠢吗?为什么这小段代码不能直接工作?

克瑞克SB

operator[]std::map<K, T>要求T是缺省构造的。

使用此代替:

m.emplace(0, std::cref(a));

在C ++ 17中,您还可以使用以下之一:

m.try_emplace(0, std::cref(a));
m.insert_or_assign(0, std::cref(a));

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

std :: reference_wrapper的问题

来自分类Dev

std :: reference_wrapper的实现

来自分类Dev

std :: reference_wrapper与int&

来自分类Dev

使用 std::bind 和 std::reference_wrapper::get

来自分类Dev

将std :: reference_wrapper <Derived>视为std :: reference_wrapper <Base>的最佳方法

来自分类Dev

std::reference_wrapper<std::any> 上的类型特征

来自分类Dev

MS Visual Studio 2013上的std :: reference_wrapper

来自分类Dev

Eigen :: Ref <>与std :: reference_wrapper <>的优缺点是什么?

来自分类Dev

std :: reference_wrapper解开包装器

来自分类Dev

在特征类型的std :: reference_wrapper中获取原始类型

来自分类Dev

MS Visual Studio 2013上的std :: reference_wrapper

来自分类Dev

如何将std :: vector <std :: reference_wrapper <T>>转换为std :: vector <T>

来自分类Dev

为什么std :: hash不专门用于std :: reference_wrapper?

来自分类Dev

std :: reference_wrapper在std :: bind上不能很好地工作

来自分类Dev

我可以实例化std :: reference_wrapper <T>,其中T是不完整的类型吗?

来自分类Dev

std :: reference_wrapper是否类似于python的传递对象引用?

来自分类Dev

为什么std :: reference_wrapper显式定义副本分配运算符?

来自分类Dev

C ++中std :: reference_wrapper和类型的编译器错误

来自分类Dev

为什么std :: make_tuple将std :: reference_wrapper <X>参数转换为X&?

来自分类Dev

为了实现std :: ref工作的唯一目的,std :: reference_wrapper实现的症结是什么?

来自分类Dev

使用`std :: reference_wrapper <T>`作为始终有效的成员变量而不是指针有什么弊端吗?

来自分类Dev

为什么在调用成员函数时std :: reference_wrapper没有隐式转换为引用?

来自分类Dev

使用reference_wrapper的常量正确性

来自分类Dev

C ++ reference_wrapper矢量使用错误的值填充

来自分类Dev

使用reference_wrapper的常量正确性

来自分类Dev

C++如何使用reference_wrapper的向量

来自分类Dev

C ++在std :: map <>中使用std :: set <>

来自分类Dev

在std :: map中使用std :: function

来自分类Dev

C ++在std :: map <>中使用std :: set <>

Related 相关文章

  1. 1

    std :: reference_wrapper的问题

  2. 2

    std :: reference_wrapper的实现

  3. 3

    std :: reference_wrapper与int&

  4. 4

    使用 std::bind 和 std::reference_wrapper::get

  5. 5

    将std :: reference_wrapper <Derived>视为std :: reference_wrapper <Base>的最佳方法

  6. 6

    std::reference_wrapper<std::any> 上的类型特征

  7. 7

    MS Visual Studio 2013上的std :: reference_wrapper

  8. 8

    Eigen :: Ref <>与std :: reference_wrapper <>的优缺点是什么?

  9. 9

    std :: reference_wrapper解开包装器

  10. 10

    在特征类型的std :: reference_wrapper中获取原始类型

  11. 11

    MS Visual Studio 2013上的std :: reference_wrapper

  12. 12

    如何将std :: vector <std :: reference_wrapper <T>>转换为std :: vector <T>

  13. 13

    为什么std :: hash不专门用于std :: reference_wrapper?

  14. 14

    std :: reference_wrapper在std :: bind上不能很好地工作

  15. 15

    我可以实例化std :: reference_wrapper <T>,其中T是不完整的类型吗?

  16. 16

    std :: reference_wrapper是否类似于python的传递对象引用?

  17. 17

    为什么std :: reference_wrapper显式定义副本分配运算符?

  18. 18

    C ++中std :: reference_wrapper和类型的编译器错误

  19. 19

    为什么std :: make_tuple将std :: reference_wrapper <X>参数转换为X&?

  20. 20

    为了实现std :: ref工作的唯一目的,std :: reference_wrapper实现的症结是什么?

  21. 21

    使用`std :: reference_wrapper <T>`作为始终有效的成员变量而不是指针有什么弊端吗?

  22. 22

    为什么在调用成员函数时std :: reference_wrapper没有隐式转换为引用?

  23. 23

    使用reference_wrapper的常量正确性

  24. 24

    C ++ reference_wrapper矢量使用错误的值填充

  25. 25

    使用reference_wrapper的常量正确性

  26. 26

    C++如何使用reference_wrapper的向量

  27. 27

    C ++在std :: map <>中使用std :: set <>

  28. 28

    在std :: map中使用std :: function

  29. 29

    C ++在std :: map <>中使用std :: set <>

热门标签

归档