unordered_map的自指使用会导致GCC 5.3但不叮当
Self-referential use of unordered_map causes problems for gcc 5.3 but not clang
以下代码无法在 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 ?
上使用。谢谢!
我建议您以下一个方式修改 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.9和GCC 6.2。
相关文章:
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 奇怪的结构&GCC&clang(void*返回类型)
- GCC本机矩阵运算库
- PowerPC ppc64le上的Gcc Woverloaded虚拟错误
- gcc和c++17的过载解析失败
- 数据成员SFINAE的C++17测试:gcc vs clang
- 叮当不知道PTRDIFF_MAX?
- GCC对可能有效的代码抛出init list生存期警告
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 使用 GCC 卸载的 OpenMP 卸载失败,并出现"Ptx assembly aborted due to errors"
- 为什么与常规GCC不同,即使有"学究性错误",MinGW-GCC也能容忍丢失的返回类型
- 使用gcc从静态链接的文件中查找可选符号
- MacOS通过在莫哈韦"wchar.h"下破碎的自制啤酒发出叮当声
- 普通环路未使用gcc 4.8.5自动矢量化
- 有了gcc,是否可以链接库,但前提是它存在
- 为什么 gcc 会产生这种奇怪的组件与叮当声?
- static_cast为仅移动类型的叮当与 GCC
- unordered_map的自指使用会导致GCC 5.3但不叮当
- 部分专用化的默认参数 [叮当是,GCC 否]
- 静态表生成适用于 GCC,但不适用于 clang;被叮当窃听