【问题标题】:Recursive unordered_map递归无序映射
【发布时间】:2020-11-01 17:59:45
【问题描述】:

我有一个内部使用无序映射的树结构

#include <unordered_map>

struct Node {
        std::unordered_map<int, Node> children;
};

int main() {
        Node a;
}

它在 Apple clang 11.0.3 和 MSVC v19.24 上运行良好,但在 clang 10.0.0 和 gcc 10.1 上编译失败

虽然常规的 std::map 在所有编译器上都可以正常工作。我没有找到造成这种差异的原因。有什么方法可以将std::unordered_map 用作自身的价值吗?或者指针是这里唯一的解决方案?

这是编译器资源管理器链接https://godbolt.org/z/6eYch9

这是来自 gcc 的错误:

    #3 with x86-64 gcc 10.1
    In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/unordered_map:43,
                    from <source>:1:
    /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_pair.h:
In instantiation of 'struct std::pair<const int, Node>':
    
    /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/aligned_buffer.h:91:28:
required from 'struct __gnu_cxx::__aligned_buffer<std::pair<const int,
Node> >'
    
    /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/hashtable_policy.h:233:43:
required from 'struct
std::__detail::_Hash_node_value_base<std::pair<const int, Node> >'
    
    /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/hashtable_policy.h:279:12:
required from 'struct std::__detail::_Hash_node<std::pair<const int,
Node>, false>'
    
    /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/hashtable_policy.h:1973:13:
required from 'struct
std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const
int, Node>, false> > >'
    
    /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/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> >'
    
    /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/unordered_map.h:105:18:
required from 'class std::unordered_map<int, Node>'

    <source>:4:39:   required from here
    /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_pair.h:218:11:
error: 'std::pair<_T1, _T2>::second' has incomplete type

      218 |       _T2 second;                ///< The second member
          |           ^~~~~~
    <source>:3:8: note: forward declaration of 'struct Node'
        3 | struct Node {
          |        ^~~~
    Compiler returned: 1

【问题讨论】:

    标签: c++ c++11 gcc clang


    【解决方案1】:

    STL 容器不需要处理不完整的类型。如果您不介意额外的间接性,那么解决方法是std::map&lt;int, std::unique_ptr&lt;Node&gt;&gt;

    【讨论】:

    • 你也可以使用 Boost.Container。
    【解决方案2】:

    这和做 e.g. 的问题是一样的

    struct Node
    {
        Node child;  // An instance of the full structure
    };
    

    在结构(或类)被完全定义之前,您不能使用它,它位于结尾}

    但是你可以定义结构的指针,因为编译器不需要完整的结构定义,只知道结构的名称:

    struct Node
    {
        Node* child;  // Pointer to the structure
    };
    

    所以要解决你的问题,你需要一张指针的地图:

    std::unordered_map<int, Node*> children;
    

    【讨论】:

    • 既然节点拥有它的子节点,那应该是std::unique_ptr&lt;Node&gt;,不是吗?
    • 这并不能解释差异
    • @Teivaz 另一个答案也是如此。可能由于某些映射碰巧在某些编译器/标准库中内部实现,它们可以使用不完整的类型作为模板参数。但他们不需要,所以你不能依赖他们现在是否看起来。
    【解决方案3】:

    在编写 Trie 数据结构时遇到此问题,我注意到在最新的 clang (v14) 和 GCC (v12) 中我没有收到错误,我的代码按预期运行,但低于这些版本的任何内容(在godbolt.com)我得到了同样的错误。这都是使用-std=c++14,所以应该和标准没有关系。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2011-07-24
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多