c++ std::unordered_map初始化后赋值编译错误

kmbjn2e3  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(211)

我在尝试从std::unordered_map继承时遇到了一个非常奇怪的错误。
在这段代码中,我只想将std::unordered<>Map到一个类中,以便用它简单地编写代码。

  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <functional>
  4. struct Vec2 {
  5. float x=0;
  6. float y=0;
  7. };
  8. const std::function<unsigned long(const Vec2&)> hash =
  9. [](const Vec2&v){
  10. return std::hash<float>()(v.x) | std::hash<float>()(v.y);
  11. };
  12. const std::function<bool (const Vec2&, const Vec2&)> equal =
  13. [](const Vec2&a, const Vec2&b){
  14. return (a.x==b.x && a.y==b.y) || (a.x==b.y && a.y==b.x);
  15. };
  16. using __Vec2Map = std::unordered_map<
  17. Vec2, float,
  18. decltype(hash), decltype(equal)
  19. >;
  20. class Vec2Map : public __Vec2Map {
  21. public:
  22. Vec2Map(uint buckets=0)
  23. : __Vec2Map(buckets, hash, equal)
  24. {}
  25. using __Vec2Map::unordered_map; // for non virtual destructors
  26. };
  27. int main() {
  28. Vec2Map ms = Vec2Map(uint(5));
  29. // ...
  30. ms = Vec2Map(uint(5));
  31. }

字符串
编译时:

  • 直接初始化通过编译(Vec 2 Map ms 1 = Vec 2 Map(uint(5));)
  • 但赋值并没有,并给出了这个长错误:
  1. In file included from /usr/include/c++/9/unordered_map:46,
  2. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
  3. /usr/include/c++/9/bits/hashtable.h: In instantiation of void std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_M_move_assign(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&, std::true_type) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::true_type = std::integral_constant<bool, true>]’:
  4. /usr/include/c++/9/bits/hashtable.h:513:2: required from std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>& std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::operator=(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>]’
  5. /usr/include/c++/9/bits/unordered_map.h:102:11: required from here
  6. /usr/include/c++/9/bits/hashtable.h:1214:34: error: use of deleted function std::__detail::_Hashtable_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<bool(const Vec2&, const Vec2&)>, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >& std::__detail::_Hashtable_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<bool(const Vec2&, const Vec2&)>, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >::operator=(std::__detail::_Hashtable_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<bool(const Vec2&, const Vec2&)>, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >&&)’
  7. 1214 | __hashtable_base::operator=(std::move(__ht));
  8. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
  9. In file included from /usr/include/c++/9/bits/hashtable.h:35,
  10. from /usr/include/c++/9/unordered_map:46,
  11. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
  12. /usr/include/c++/9/bits/hashtable_policy.h:1770:10: note: std::__detail::_Hashtable_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<bool(const Vec2&, const Vec2&)>, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >& std::__detail::_Hashtable_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<bool(const Vec2&, const Vec2&)>, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >::operator=(std::__detail::_Hashtable_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<bool(const Vec2&, const Vec2&)>, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >&&)’ is implicitly deleted because the default definition would be ill-formed:
  13. 1770 | struct _Hashtable_base
  14. | ^~~~~~~~~~~~~~~
  15. /usr/include/c++/9/bits/hashtable_policy.h:1770:10: error: use of deleted function std::__detail::_Hash_code_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>& std::__detail::_Hash_code_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::operator=(std::__detail::_Hash_code_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>&&)’
  16. /usr/include/c++/9/bits/hashtable_policy.h:1346:12: note: std::__detail::_Hash_code_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>& std::__detail::_Hash_code_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>::operator=(std::__detail::_Hash_code_base<Vec2, std::pair<const Vec2, float>, std::__detail::_Select1st, const std::function<long unsigned int(const Vec2&)>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>&&)’ is implicitly deleted because the default definition would be ill-formed:
  17. 1346 | struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
  18. | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. 1347 | _Default_ranged_hash, true>
  20. | ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  21. /usr/include/c++/9/bits/hashtable_policy.h:1346:12: error: use of deleted function std::__detail::_Hashtable_ebo_helper<1, const std::function<long unsigned int(const Vec2&)>, false>& std::__detail::_Hashtable_ebo_helper<1, const std::function<long unsigned int(const Vec2&)>, false>::operator=(std::__detail::_Hashtable_ebo_helper<1, const std::function<long unsigned int(const Vec2&)>, false>&&)’
  22. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: note: std::__detail::_Hashtable_ebo_helper<1, const std::function<long unsigned int(const Vec2&)>, false>& std::__detail::_Hashtable_ebo_helper<1, const std::function<long unsigned int(const Vec2&)>, false>::operator=(std::__detail::_Hashtable_ebo_helper<1, const std::function<long unsigned int(const Vec2&)>, false>&&)’ is implicitly deleted because the default definition would be ill-formed:
  23. 1114 | struct _Hashtable_ebo_helper<_Nm, _Tp, false>
  24. | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  25. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: error: no matching function for call to std::function<long unsigned int(const Vec2&)>::operator=(const std::function<long unsigned int(const Vec2&)>) const
  26. In file included from /usr/include/c++/9/functional:59,
  27. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:3:
  28. /usr/include/c++/9/bits/std_function.h:462:7: note: candidate: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(const std::function<_Res(_ArgTypes ...)>&) [with _Res = long unsigned int; _ArgTypes = {const Vec2&}]’ <near match>
  29. 462 | operator=(const function& __x)
  30. | ^~~~~~~~
  31. /usr/include/c++/9/bits/std_function.h:462:7: note: passing const std::function<long unsigned int(const Vec2&)>*’ as this argument discards qualifiers
  32. /usr/include/c++/9/bits/std_function.h:480:7: note: candidate: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::function<_Res(_ArgTypes ...)>&&) [with _Res = long unsigned int; _ArgTypes = {const Vec2&}]’ <near match>
  33. 480 | operator=(function&& __x) noexcept
  34. | ^~~~~~~~
  35. /usr/include/c++/9/bits/std_function.h:480:7: note: conversion of argument 1 would be ill-formed:
  36. In file included from /usr/include/c++/9/bits/hashtable.h:35,
  37. from /usr/include/c++/9/unordered_map:46,
  38. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
  39. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: error: binding reference of type std::function<long unsigned int(const Vec2&)>&&’ to const std::function<long unsigned int(const Vec2&)>’ discards qualifiers
  40. 1114 | struct _Hashtable_ebo_helper<_Nm, _Tp, false>
  41. | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  42. In file included from /usr/include/c++/9/functional:59,
  43. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:3:
  44. /usr/include/c++/9/bits/std_function.h:494:7: note: candidate: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::nullptr_t) [with _Res = long unsigned int; _ArgTypes = {const Vec2&}; std::nullptr_t = std::nullptr_t]’
  45. 494 | operator=(nullptr_t) noexcept
  46. | ^~~~~~~~
  47. /usr/include/c++/9/bits/std_function.h:523:2: note: candidate: template<class _Functor> std::function<_Res(_ArgTypes ...)>::_Requires<std::function<_Res(_ArgTypes ...)>::_Callable<typename std::decay<_Up>::type>, std::function<_Res(_ArgTypes ...)>&> std::function<_Res(_ArgTypes ...)>::operator=(_Functor&&) [with _Functor = _Functor; _Res = long unsigned int; _ArgTypes = {const Vec2&}]’
  48. 523 | operator=(_Functor&& __f)
  49. | ^~~~~~~~
  50. /usr/include/c++/9/bits/std_function.h:523:2: note: template argument deduction/substitution failed:
  51. /usr/include/c++/9/bits/std_function.h: In substitution of template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using _Requires = typename std::enable_if<_Cond::value, _Tp>::type [with _Cond = std::function<long unsigned int(const Vec2&)>::_Callable<std::function<long unsigned int(const Vec2&)>, std::__invoke_result<std::function<long unsigned int(const Vec2&)>&, const Vec2&> >; _Tp = std::function<long unsigned int(const Vec2&)>&; _Res = long unsigned int; _ArgTypes = {const Vec2&}]’:
  52. /usr/include/c++/9/bits/std_function.h:523:2: required by substitution of template<class _Functor> std::function<long unsigned int(const Vec2&)>::_Requires<std::function<long unsigned int(const Vec2&)>::_Callable<typename std::decay<_Tp>::type, std::__invoke_result<typename std::decay<_Tp>::type&, const Vec2&> >, std::function<long unsigned int(const Vec2&)>&> std::function<long unsigned int(const Vec2&)>::operator=<_Functor>(_Functor&&) [with _Functor = const std::function<long unsigned int(const Vec2&)>]’
  53. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: required from void std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_M_move_assign(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&, std::true_type) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::true_type = std::integral_constant<bool, true>]’
  54. /usr/include/c++/9/bits/hashtable.h:513:2: required from std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>& std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::operator=(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>]’
  55. /usr/include/c++/9/bits/unordered_map.h:102:11: required from here
  56. /usr/include/c++/9/bits/std_function.h:385:8: error: no type named type in struct std::enable_if<false, std::function<long unsigned int(const Vec2&)>&>’
  57. 385 | using _Requires = typename enable_if<_Cond::value, _Tp>::type;
  58. | ^~~~~~~~~
  59. /usr/include/c++/9/bits/hashtable_policy.h: In instantiation of void std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_M_move_assign(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&, std::true_type) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::true_type = std::integral_constant<bool, true>]’:
  60. /usr/include/c++/9/bits/hashtable.h:513:2: required from std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>& std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::operator=(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>]’
  61. /usr/include/c++/9/bits/unordered_map.h:102:11: required from here
  62. /usr/include/c++/9/bits/std_function.h:532:2: note: candidate: template<class _Functor> std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::reference_wrapper<_Functor>) [with _Functor = _Functor; _Res = long unsigned int; _ArgTypes = {const Vec2&}]’
  63. 532 | operator=(reference_wrapper<_Functor> __f) noexcept
  64. | ^~~~~~~~
  65. /usr/include/c++/9/bits/std_function.h:532:2: note: template argument deduction/substitution failed:
  66. In file included from /usr/include/c++/9/bits/hashtable.h:35,
  67. from /usr/include/c++/9/unordered_map:46,
  68. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
  69. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: note: std::function<long unsigned int(const Vec2&)>’ is not derived from std::reference_wrapper<_Tp>’
  70. 1114 | struct _Hashtable_ebo_helper<_Nm, _Tp, false>
  71. | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. /usr/include/c++/9/bits/hashtable_policy.h:1770:10: error: use of deleted function std::__detail::_Hashtable_ebo_helper<0, const std::function<bool(const Vec2&, const Vec2&)>, false>& std::__detail::_Hashtable_ebo_helper<0, const std::function<bool(const Vec2&, const Vec2&)>, false>::operator=(std::__detail::_Hashtable_ebo_helper<0, const std::function<bool(const Vec2&, const Vec2&)>, false>&&)’
  73. 1770 | struct _Hashtable_base
  74. | ^~~~~~~~~~~~~~~
  75. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: note: std::__detail::_Hashtable_ebo_helper<0, const std::function<bool(const Vec2&, const Vec2&)>, false>& std::__detail::_Hashtable_ebo_helper<0, const std::function<bool(const Vec2&, const Vec2&)>, false>::operator=(std::__detail::_Hashtable_ebo_helper<0, const std::function<bool(const Vec2&, const Vec2&)>, false>&&)’ is implicitly deleted because the default definition would be ill-formed:
  76. 1114 | struct _Hashtable_ebo_helper<_Nm, _Tp, false>
  77. | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: error: no matching function for call to std::function<bool(const Vec2&, const Vec2&)>::operator=(const std::function<bool(const Vec2&, const Vec2&)>) const
  79. In file included from /usr/include/c++/9/functional:59,
  80. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:3:
  81. /usr/include/c++/9/bits/std_function.h:462:7: note: candidate: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(const std::function<_Res(_ArgTypes ...)>&) [with _Res = bool; _ArgTypes = {const Vec2&, const Vec2&}]’ <near match>
  82. 462 | operator=(const function& __x)
  83. | ^~~~~~~~
  84. /usr/include/c++/9/bits/std_function.h:462:7: note: passing const std::function<bool(const Vec2&, const Vec2&)>*’ as this argument discards qualifiers
  85. /usr/include/c++/9/bits/std_function.h:480:7: note: candidate: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::function<_Res(_ArgTypes ...)>&&) [with _Res = bool; _ArgTypes = {const Vec2&, const Vec2&}]’ <near match>
  86. 480 | operator=(function&& __x) noexcept
  87. | ^~~~~~~~
  88. /usr/include/c++/9/bits/std_function.h:480:7: note: conversion of argument 1 would be ill-formed:
  89. In file included from /usr/include/c++/9/bits/hashtable.h:35,
  90. from /usr/include/c++/9/unordered_map:46,
  91. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
  92. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: error: binding reference of type std::function<bool(const Vec2&, const Vec2&)>&&’ to const std::function<bool(const Vec2&, const Vec2&)>’ discards qualifiers
  93. 1114 | struct _Hashtable_ebo_helper<_Nm, _Tp, false>
  94. | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  95. In file included from /usr/include/c++/9/functional:59,
  96. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:3:
  97. /usr/include/c++/9/bits/std_function.h:494:7: note: candidate: std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::nullptr_t) [with _Res = bool; _ArgTypes = {const Vec2&, const Vec2&}; std::nullptr_t = std::nullptr_t]’
  98. 494 | operator=(nullptr_t) noexcept
  99. | ^~~~~~~~
  100. /usr/include/c++/9/bits/std_function.h:523:2: note: candidate: template<class _Functor> std::function<_Res(_ArgTypes ...)>::_Requires<std::function<_Res(_ArgTypes ...)>::_Callable<typename std::decay<_Up>::type>, std::function<_Res(_ArgTypes ...)>&> std::function<_Res(_ArgTypes ...)>::operator=(_Functor&&) [with _Functor = _Functor; _Res = bool; _ArgTypes = {const Vec2&, const Vec2&}]’
  101. 523 | operator=(_Functor&& __f)
  102. | ^~~~~~~~
  103. /usr/include/c++/9/bits/std_function.h:523:2: note: template argument deduction/substitution failed:
  104. /usr/include/c++/9/bits/std_function.h: In substitution of template<class _Res, class ... _ArgTypes> template<class _Cond, class _Tp> using _Requires = typename std::enable_if<_Cond::value, _Tp>::type [with _Cond = std::function<bool(const Vec2&, const Vec2&)>::_Callable<std::function<bool(const Vec2&, const Vec2&)>, std::__invoke_result<std::function<bool(const Vec2&, const Vec2&)>&, const Vec2&, const Vec2&> >; _Tp = std::function<bool(const Vec2&, const Vec2&)>&; _Res = bool; _ArgTypes = {const Vec2&, const Vec2&}]’:
  105. /usr/include/c++/9/bits/std_function.h:523:2: required by substitution of template<class _Functor> std::function<bool(const Vec2&, const Vec2&)>::_Requires<std::function<bool(const Vec2&, const Vec2&)>::_Callable<typename std::decay<_Tp>::type, std::__invoke_result<typename std::decay<_Tp>::type&, const Vec2&, const Vec2&> >, std::function<bool(const Vec2&, const Vec2&)>&> std::function<bool(const Vec2&, const Vec2&)>::operator=<_Functor>(_Functor&&) [with _Functor = const std::function<bool(const Vec2&, const Vec2&)>]’
  106. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: required from void std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_M_move_assign(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&, std::true_type) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::true_type = std::integral_constant<bool, true>]’
  107. /usr/include/c++/9/bits/hashtable.h:513:2: required from std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>& std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::operator=(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>]’
  108. /usr/include/c++/9/bits/unordered_map.h:102:11: required from here
  109. /usr/include/c++/9/bits/std_function.h:385:8: error: no type named type in struct std::enable_if<false, std::function<bool(const Vec2&, const Vec2&)>&>’
  110. 385 | using _Requires = typename enable_if<_Cond::value, _Tp>::type;
  111. | ^~~~~~~~~
  112. /usr/include/c++/9/bits/hashtable_policy.h: In instantiation of void std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::_M_move_assign(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&, std::true_type) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::true_type = std::integral_constant<bool, true>]’:
  113. /usr/include/c++/9/bits/hashtable.h:513:2: required from std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>& std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>::operator=(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _H1, _H2, _Hash, _RehashPolicy, _Traits>&&) [with _Key = Vec2; _Value = std::pair<const Vec2, float>; _Alloc = std::allocator<std::pair<const Vec2, float> >; _ExtractKey = std::__detail::_Select1st; _Equal = const std::function<bool(const Vec2&, const Vec2&)>; _H1 = const std::function<long unsigned int(const Vec2&)>; _H2 = std::__detail::_Mod_range_hashing; _Hash = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>]’
  114. /usr/include/c++/9/bits/unordered_map.h:102:11: required from here
  115. /usr/include/c++/9/bits/std_function.h:532:2: note: candidate: template<class _Functor> std::function<_Res(_ArgTypes ...)>& std::function<_Res(_ArgTypes ...)>::operator=(std::reference_wrapper<_Functor>) [with _Functor = _Functor; _Res = bool; _ArgTypes = {const Vec2&, const Vec2&}]’
  116. 532 | operator=(reference_wrapper<_Functor> __f) noexcept
  117. | ^~~~~~~~
  118. /usr/include/c++/9/bits/std_function.h:532:2: note: template argument deduction/substitution failed:
  119. In file included from /usr/include/c++/9/bits/hashtable.h:35,
  120. from /usr/include/c++/9/unordered_map:46,
  121. from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
  122. /usr/include/c++/9/bits/hashtable_policy.h:1114:12: note: std::function<bool(const Vec2&, const Vec2&)>’ is not derived from std::reference_wrapper<_Tp>’
  123. 1114 | struct _Hashtable_ebo_helper<_Nm, _Tp, false>
  124. | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  125. make[2]: *** [CMakeFiles/test.dir/build.make:76 : CMakeFiles/test.dir/main.cpp.o] Erreur 1
  126. make[1]: *** [CMakeFiles/Makefile2:83 : CMakeFiles/test.dir/all] Erreur 2
  127. make: *** [Makefile:91 : all] Erreur 2


我寻找关于构造函数和赋值运算符的说明,但什么也没找到。
我可以在初始化后使用它并避免赋值,但我真的想知道为什么会发生这个错误。
谢谢你的帮助

vof42yt1

vof42yt11#

散列器和比较类型包括来自hashequal声明的**const s**,因此std::unordered_map存储const限定的子对象,并且不可赋值。
(The继承与此无关:同样的事情只会发生在类型别名上。

相关问题