首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >unordered_map的自我引用使用给gcc 5.3带来的问题,而不是clang。

unordered_map的自我引用使用给gcc 5.3带来的问题,而不是clang。
EN

Stack Overflow用户
提问于 2016-12-15 21:12:32
回答 1查看 394关注 0票数 5

下面的代码无法在gcc 5.3下编译(这是从更大的代码中提取的简化版本):

代码语言:javascript
运行
复制
#include <unordered_map>
#include <string>

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

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

有以下错误:

代码语言:javascript
运行
复制
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++的代码工作,我还能做什么修改吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-16 10:59:12

我建议您以下一种方式修改Foo类:

代码语言:javascript
运行
复制
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中都编译得很好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41173415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档