【发布时间】:2015-05-08 18:42:34
【问题描述】:
考虑与上一个 SO 问题 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 编译。
g++吐出的错误以
开头
error: invalid application of 'sizeof' to incomplete type 'Node': std::aligned_storage<sizeof(_Tp), std::alignment_of<_Tp>::value>
问题:在声明std::unordered_set 时,Node 是否必须是完整类型?在这种情况下,看起来 g++ 或 clang++ 都是错误的。
PS:我知道这种情况可以通过使用std::shared_ptr<Node> 来避免,但是想了解上面代码中的行为。
【问题讨论】:
标签: c++ c++11 clang incomplete-type gcc5