与unordered_map相关的类型 [英] Codependent types with unordered_map

查看:173
本文介绍了与unordered_map相关的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要在unordered_map< int,int>的条目
之间保持一定的顺序。一种高效的内存存储方式似乎是在映射条目之间保留一个链表。即,我将使用unordered_map 而不是unordered_map< int,int>。其中Node定义为

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;
};

这是有效的C ++吗? Clang和gcc不允许这样说Node是不完整的类型。有关完整的错误消息,请参见下文。两者都接受以下内容:

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;
};

这里的规则到底是什么?为什么第一个不被接受但是第二个很好呢?在一个相关的问题中,出现了类似的问题,需要同样的解释在这里,必须保证unordered_map包含一个Value对象而没有任何间接调用。即,unordered_map的定义应如下所示:

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*;
};

我不明白为什么unordered_map应该直接存储值。否则,
就像我在上面给出的玩具示例struct Map中一样,没有依赖周期。

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 {


推荐答案

A,

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

不是有效的C ++。向 std :: vector std :: list 提供不完整的类型 Node code>和 std :: forward_list 根据最近接受的不完整类型建议到C ++ 17,但是unordered_map仍将提供完整类型。

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和libc ++可以很好地使用不完整的值,但这是由于它们超出了标准的要求。

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

这篇关于与unordered_map相关的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆