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

kmbjn2e3  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(194)

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

#include <iostream>
#include <unordered_map>
#include <functional>

struct Vec2 {
  float x=0;
  float y=0;
};

const std::function<unsigned long(const Vec2&)> hash = 
  [](const Vec2&v){
    return std::hash<float>()(v.x) | std::hash<float>()(v.y);
  };
const std::function<bool (const Vec2&, const Vec2&)> equal = 
  [](const Vec2&a, const Vec2&b){
    return (a.x==b.x && a.y==b.y) || (a.x==b.y && a.y==b.x);
  };

using __Vec2Map = std::unordered_map<
  Vec2, float,
  decltype(hash), decltype(equal)
>;
class Vec2Map : public __Vec2Map {
public:
  Vec2Map(uint buckets=0)
  : __Vec2Map(buckets, hash, equal)
  {}
  using __Vec2Map::unordered_map; // for non virtual destructors
};

int main() {
  Vec2Map ms = Vec2Map(uint(5));

  // ...

  ms = Vec2Map(uint(5));
}

字符串
编译时:

  • 直接初始化通过编译(Vec 2 Map ms 1 = Vec 2 Map(uint(5));)
  • 但赋值并没有,并给出了这个长错误:
In file included from /usr/include/c++/9/unordered_map:46,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
/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>]’:
/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>]’
/usr/include/c++/9/bits/unordered_map.h:102:11:   required from here
/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> >&&)’
 1214 |       __hashtable_base::operator=(std::move(__ht));
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/bits/hashtable.h:35,
                 from /usr/include/c++/9/unordered_map:46,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
/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:
 1770 |   struct _Hashtable_base
      |          ^~~~~~~~~~~~~~~
/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>&&)’
/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:
 1346 |     struct _Hash_code_base<_Key, _Value, _ExtractKey, _H1, _H2,
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1347 |       _Default_ranged_hash, true>
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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>&&)’
/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:
 1114 |     struct _Hashtable_ebo_helper<_Nm, _Tp, false>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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’
In file included from /usr/include/c++/9/functional:59,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:3:
/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>
  462 |       operator=(const function& __x)
      |       ^~~~~~~~
/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
/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>
  480 |       operator=(function&& __x) noexcept
      |       ^~~~~~~~
/usr/include/c++/9/bits/std_function.h:480:7: note:   conversion of argument 1 would be ill-formed:
In file included from /usr/include/c++/9/bits/hashtable.h:35,
                 from /usr/include/c++/9/unordered_map:46,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
/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
 1114 |     struct _Hashtable_ebo_helper<_Nm, _Tp, false>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/functional:59,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:3:
/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]’
  494 |       operator=(nullptr_t) noexcept
      |       ^~~~~~~~
/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&}]’
  523 |  operator=(_Functor&& __f)
      |  ^~~~~~~~
/usr/include/c++/9/bits/std_function.h:523:2: note:   template argument deduction/substitution failed:
/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&}]’:
/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&)>]’
/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>]’
/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>]’
/usr/include/c++/9/bits/unordered_map.h:102:11:   required from here
/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&)>&>’
  385 |  using _Requires = typename enable_if<_Cond::value, _Tp>::type;
      |        ^~~~~~~~~
/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>]’:
/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>]’
/usr/include/c++/9/bits/unordered_map.h:102:11:   required from here
/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&}]’
  532 |  operator=(reference_wrapper<_Functor> __f) noexcept
      |  ^~~~~~~~
/usr/include/c++/9/bits/std_function.h:532:2: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/9/bits/hashtable.h:35,
                 from /usr/include/c++/9/unordered_map:46,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
/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>’
 1114 |     struct _Hashtable_ebo_helper<_Nm, _Tp, false>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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>&&)’
 1770 |   struct _Hashtable_base
      |          ^~~~~~~~~~~~~~~
/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:
 1114 |     struct _Hashtable_ebo_helper<_Nm, _Tp, false>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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’
In file included from /usr/include/c++/9/functional:59,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:3:
/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>
  462 |       operator=(const function& __x)
      |       ^~~~~~~~
/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
/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>
  480 |       operator=(function&& __x) noexcept
      |       ^~~~~~~~
/usr/include/c++/9/bits/std_function.h:480:7: note:   conversion of argument 1 would be ill-formed:
In file included from /usr/include/c++/9/bits/hashtable.h:35,
                 from /usr/include/c++/9/unordered_map:46,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
/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
 1114 |     struct _Hashtable_ebo_helper<_Nm, _Tp, false>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/9/functional:59,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:3:
/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]’
  494 |       operator=(nullptr_t) noexcept
      |       ^~~~~~~~
/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&}]’
  523 |  operator=(_Functor&& __f)
      |  ^~~~~~~~
/usr/include/c++/9/bits/std_function.h:523:2: note:   template argument deduction/substitution failed:
/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&}]’:
/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&)>]’
/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>]’
/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>]’
/usr/include/c++/9/bits/unordered_map.h:102:11:   required from here
/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&)>&>’
  385 |  using _Requires = typename enable_if<_Cond::value, _Tp>::type;
      |        ^~~~~~~~~
/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>]’:
/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>]’
/usr/include/c++/9/bits/unordered_map.h:102:11:   required from here
/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&}]’
  532 |  operator=(reference_wrapper<_Functor> __f) noexcept
      |  ^~~~~~~~
/usr/include/c++/9/bits/std_function.h:532:2: note:   template argument deduction/substitution failed:
In file included from /usr/include/c++/9/bits/hashtable.h:35,
                 from /usr/include/c++/9/unordered_map:46,
                 from /home/ghislain/projects/ghislain-kengne-gumete/sandbox/main.cpp:2:
/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>’
 1114 |     struct _Hashtable_ebo_helper<_Nm, _Tp, false>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/test.dir/build.make:76 : CMakeFiles/test.dir/main.cpp.o] Erreur 1
make[1]: *** [CMakeFiles/Makefile2:83 : CMakeFiles/test.dir/all] Erreur 2
make: *** [Makefile:91 : all] Erreur 2


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

vof42yt1

vof42yt11#

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

相关问题