【问题标题】:Self-referential use of unordered_map causes problems for gcc 5.3 but not clangunordered_map 的自引用使用会导致 gcc 5.3 出现问题,但不会导致 clang
【发布时间】:2016-12-15 21:12:32
【问题描述】:

以下代码无法在 gcc 5.3 下编译(它是从一段较大的代码中提取的简化版本):

#include <unordered_map>
#include <string>

class Foo {
    std::unordered_map<std::string, Foo> m;  //"self-referential"
};

int main()
{
    Foo f;
    return 0;
}

出现以下错误:

g++ --std=c++1y  -c rh.cpp

In file included from /usr/local/include/c++/5.3.0/utility:70:0,
                 from /usr/local/include/c++/5.3.0/unordered_map:38,
                 from rh.cpp:1:
/usr/local/include/c++/5.3.0/bits/stl_pair.h: In instantiation of ‘struct std::pair<const int, Foo>’:
/usr/local/include/c++/5.3.0/ext/aligned_buffer.h:85:34:   required from ‘struct __gnu_cxx::__aligned_buffer<std::pair<const int, Foo> >’
/usr/local/include/c++/5.3.0/bits/hashtable_policy.h:246:43:   required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const int, Foo> >’
/usr/local/include/c++/5.3.0/bits/hashtable_policy.h:292:12:   required from ‘struct std::__detail::_Hash_node<std::pair<const int, Foo>, false>’
/usr/local/include/c++/5.3.0/bits/hashtable_policy.h:1896:60:   required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const int, Foo>, false> > >’
/usr/local/include/c++/5.3.0/bits/hashtable.h:170:11:   required from ‘class std::_Hashtable<int, std::pair<const int, Foo>, std::allocator<std::pair<const int, Foo> >, 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/local/include/c++/5.3.0/bits/unordered_map.h:101:18:   required from ‘class std::unordered_map<int, Foo>’
rh.cpp:4:32:   required from here
/usr/local/include/c++/5.3.0/bits/stl_pair.h:102:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
       _T2 second;                /// @c second is a copy of the second object
           ^
rh.cpp:3:7: note: forward declaration of ‘class Foo’
 class Foo {

使用 clang 的代码没有问题(我在 Linux 上测试了 3.8,在 OSX 上测试了 3.9):

clang++ --std=c++1y --stdlib=libc++ -c rh.cpp

在 linux 上,使用 clang + libstdc++ 也会失败。

问题似乎归结为 libstdc++ 在其哈希映射实现中使用了__gnu_cxx::__aligned_buffer,它需要一个完整的类型。

当使用 std::map 时,两个标准库都可以正常工作,但这不是我可以接受的解决方案。映射的值类型也不是指向 Foo 的指针。

为了让代码在 gcc/libstdc++ 上运行,我还能做任何其他更改吗?

谢谢!

【问题讨论】:

  • 你能把m变成指针本身吗?
  • 这可能是一种方法,但我不知道如何使其发挥作用。一旦我将m 设为指针,gcc 就会很高兴,但是一旦我尝试在构造函数中调用 new,例如Foo(): m(new std::unordered_map&lt;int, Foo&gt;()) {},我也面临同样的问题。

标签: c++ gcc clang++ libstdc++ libc++


【解决方案1】:

我建议你修改Foo类的方式如下:

class Foo {
public:
    Foo();
private:
    using MapType = std::unordered_map<std::string, Foo>;
    std::shared_ptr<MapType> m_ptr;
};

// Here Foo is already defined.
Foo::Foo() {
    m_ptr = std::make_shared<MapType>();
}

此代码与clang 3.9gcc 6.2 都可以正常编译。

【讨论】:

  • 我自己会选择unique_ptr,这会强调编写自定义复制构造函数的必要性。
  • @hvd 我没有过多考虑 ptr 的类型,只是有助于避免自定义内存管理。不管怎样,主要的想法是展示如何重新排列代码以避免编译问题。
猜你喜欢
  • 2023-04-04
  • 2016-09-08
  • 2023-03-05
  • 2016-02-11
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多