不完整的类型为std :: unordered_set编译错误在g ++ 5,编译clang ++ [英] incomplete type for std::unordered_set compiling error in g++5, compiles in clang++
问题描述
请考虑与先前SO问题相关的代码与邻接列表表示形成的C ++循环依赖混淆
Consider the code related to a previous SO question C++ cyclic dependency confusion with adjacency list representation
#include <cstddef>
#include <unordered_set>
class Node;
class Hash {
public:
std::size_t operator()(const Node &node) const;
};
class Node {
public:
int data;
std::unordered_set<Node, Hash> links;
};
inline size_t Hash::operator()(const Node &node) const {
return node.data;
}
int main()
{
}
使用g ++ 4.9.2或g ++ 5时,无法编译此代码,但是使用clang ++ 3.5编译。
This code does not compile when using g++4.9.2 or g++5, however compiles with clang++3.5.
由g ++开始的错误从
The error spit out by g++ starts with
错误: Node'
:std :: aligned_storage< sizeof(_Tp),std :: alignment_of< _Tp> :: value>
p>
error: invalid application of 'sizeof' to incomplete type 'Node'
: std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>
问题:节点
必须是完整的在声明 std :: unordered_set
时键入?看起来像g ++或clang ++在这种情况下是错误的。
Question: Does Node
have to be a complete type when declaring an std::unordered_set
? Looks like either g++ or clang++ is wrong in this case.
PS:我知道这种情况可以通过使用 std :: shared_ptr&
PS: I know this situation can be avoided by using a std::shared_ptr<Node>
instead, however would like to understand the behaviour in the code above.
推荐答案
1在某些情况下(替换函数,处理函数,
用于实例化标准库模板
组件的类型),C ++标准库取决于由C ++程序提供的
组件。如果这些组件不满足他们的要求,
标准对实现没有要求。
1 In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++ standard library depends on components supplied by a C++ program. If these components do not meet their requirements, the Standard places no requirements on the implementation.
2特别是,在下列情况下,效果是未定义的:
2 In particular, the effects are undefined in the following cases:
- [...]
两者都是实例化模板组件时的参数实施正确。
Both implementations are correct.
目前有一个提案以向某些容器添加不完全类型支持,但仅限于向量
, list
和 forward_list
。
There is currently a proposal to add incomplete type support to some containers, but it is limited to vector
, list
and forward_list
.
这篇关于不完整的类型为std :: unordered_set编译错误在g ++ 5,编译clang ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!