简体   繁体   English

自引用使用unordered_map会导致gcc 5.3而不是clang出现问题

[英]Self-referential use of unordered_map causes problems for gcc 5.3 but not clang

The following code fails to compile under gcc 5.3 (it's a reduced version taken from a larger piece of code): 以下代码无法在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;
}

with the following errors: 出现以下错误:

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 {

There is no issue with the code using clang (I tested 3.8 on Linux, and 3.9 on OSX): 使用clang的代码没有问题(我在Linux上测试了3.8,在OSX上测试了3.9):

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

On linux, using clang + libstdc++ also fails. 在Linux上,使用clang + libstdc ++也会失败。

The issue seems to come down to libstdc++ using an __gnu_cxx::__aligned_buffer in its hash map implementation, which needs a complete type. 问题似乎归结为在其哈希映射实现中使用__gnu_cxx::__aligned_buffer libstdc ++ ,它需要完整的类型。

Both standard libraries work fine when a std::map is used, but that is not a solution I can accept. 当使用std :: map时,两个标准库都可以正常工作,但这不是我可以接受的解决方案。 Nor is having the value type of the map be a pointer to Foo . 映射的值类型也不是指向Foo的指针。

Is there any other change that I can make to have the code work on gcc/libstdc++ ? 为了使代码在gcc / libstdc ++上运行,我还可以进行其他更改吗?

Thanks! 谢谢!

I suggest you to modify Foo class in the next manner: 我建议您以以下方式修改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>();
}

This code compiles fine with both clang 3.9 and gcc 6.2 . 这段代码可以用clang 3.9gcc 6.2很好地编译。

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

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