简体   繁体   中英

Codependent types with unordered_map

Suppose that I want to keep a certain ordering between the entries of an 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

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

Is this valid C++? Clang and gcc do not permit this saying Node is an incomplete type. 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. Namely the definition of unordered_map should look like this:

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. Otherwise, as in the toy example struct Map I gave above, there is no dependency cycle.

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. 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.

MSVC and libc++ work fine with incomplete Value, but this is due to them going beyond the requirements of the standard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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