简体   繁体   English

具有 unordered_map 的相互依赖类型

[英]Codependent types with unordered_map

Suppose that I want to keep a certain ordering between the entries of an unordered_map<int, int>.假设我想在 unordered_map<int, int> 的条目之间保持某种顺序。 A memory efficient way to do that seems to be keeping a linked list between the entries of the map.一种内存高效的方法似乎是在地图的条目之间保留一个链表。 Namely instead of having an unordered_map<int, int>, I will use an unordered_map<int, Node> where Node is defined as也就是说,我将使用 unordered_map<int, Node> 而不是 unordered_map<int, int>,其中 Node 定义为

struct Node {
  int val;
  typename std::unordered_map<int, Node>::iterator up;
};

Is this valid C++?这是有效的 C++ 吗? Clang and gcc do not permit this saying Node is an incomplete type. Clang 和 gcc 不允许这样说 Node 是不完整的类型。 See below for the full error message.请参阅下面的完整错误消息。 The following is accepted by both:以下为双方所接受:

template<typename Key, typename Value>
struct Map {
  struct MapEntry {
    Key key;
    Value val;
    MapEntry *prev, *next;
  };

  using Iterator = MapEntry*;
};

struct Node {
  int val;
  Map<int, Node>::Iterator up;
};

What exactly is the rule here?这里的规则究竟是什么? Why is it that the first is not accepted but the second is fine?为什么第一个不被接受但第二个很好? In a related question , a similar issue arose, however, for the same explanation to apply here it must be that unordered_map contains a Value object without any indirection.在一个相关的问题中,出现了类似的问题,但是,对于此处适用的相同解释,必须是 unordered_map 包含一个没有任何间接性的 Value 对象。 Namely the definition of unordered_map should look like this:即 unordered_map 的定义应该是这样的:

template<typename Key, typename Value>
class unordered_map {
  Value val;
  using value_type = std::pair<const Key, Value>;
  using entry = std::tuple<value_type, entry*, entry*>;
  using iterator = entry*;
};

I don't see why unordered_map should store a Value directly.我不明白为什么 unordered_map 应该直接存储一个值。 Otherwise, as in the toy example struct Map I gave above, there is no dependency cycle.否则,就像我上面给出的玩具示例 struct Map 一样,没有依赖循环。

In file included from /usr/include/c++/8/unordered_map:43,
                 from test.cpp:1:
/usr/include/c++/8/bits/stl_pair.h: In instantiation of ‘struct std::pair<const int, Node>’:
/usr/include/c++/8/ext/aligned_buffer.h:91:28:   required from ‘struct __gnu_cxx::__aligned_buffer<std::pair<const int, Node> >’
/usr/include/c++/8/bits/hashtable_policy.h:234:43:   required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const int, Node> >’
/usr/include/c++/8/bits/hashtable_policy.h:280:12:   required from ‘struct std::__detail::_Hash_node<std::pair<const int, Node>, false>’
/usr/include/c++/8/bits/hashtable_policy.h:2027:49:   required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const int, Node>, false> > >’
/usr/include/c++/8/bits/hashtable.h:173:11:   required from ‘class std::_Hashtable<int, std::pair<const int, Node>, std::allocator<std::pair<const int, Node> >, std::__detail::_Select1st, std::equal_to<int>, std::hash<int>, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<false, false, true> >’
/usr/include/c++/8/bits/unordered_map.h:105:18:   required from ‘class std::unordered_map<int, Node>’
test.cpp:5:32:   required from here
/usr/include/c++/8/bits/stl_pair.h:215:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
       _T2 second;                /// @c second is a copy of the second object
           ^~~~~~
test.cpp:3:8: note: forward declaration of ‘struct Node’
 struct Node {

Alas,唉,

struct Node {
  int val;
  typename std::unordered_map<int, Node>::iterator up;
};

is not valid C++ as of C++17 standard.不是C++17 标准的有效 C++。 Providing the incomplete type Node to a std::vector , std::list and std::forward_list is valid as per the recent acceptance of the incomplete types proposal into C++17, but unordered_map is still to be provided with complete types.将不完整类型Node提供给std::vectorstd::liststd::forward_list是有效的,因为最近 C++17 接受了不完整类型提案,但 unordered_map 仍需提供完整类型。

MSVC and libc++ work fine with incomplete Value, but this is due to them going beyond the requirements of the standard. MSVC 和 libc++ 可以在不完整的 Value 下正常工作,但这是因为它们超出了标准的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM