我在编译 llvm 时遇到问题。问题是我当前的编译器(clang + libc++)试图在模板参数被定义之前实例化一个模板。这是代码示例:
// ----- TYPEDEFS -----
class NodeEntry;
class EdgeEntry;
typedef std::list<NodeEntry> NodeList;
typedef std::list<EdgeEntry> EdgeList;
typedef NodeList::iterator NodeItr; // line 39
typedef NodeList::const_iterator ConstNodeItr;
typedef EdgeList::iterator EdgeItr;
typedef EdgeList::const_iterator ConstEdgeItr;
typedef std::list<EdgeItr> AdjEdgeList;
typedef AdjEdgeList::iterator AdjEdgeItr;
class NodeEntry {
private:
AdjEdgeList adjEdges;
...
};
class EdgeEntry {
private:
AdjEdgeItr node1AEItr, node2AEItr;
...
};
编译器的错误是这样的:
error: field has incomplete type 'PBQP::Graph::NodeEntry'
/Developer/Extras/llvm/include/llvm/CodeGen/PBQP/Graph.h:39:13: note: in instantiation of template class
'std::__1::list<PBQP::Graph::NodeEntry, std::__1::allocator<PBQP::Graph::NodeEntry> >' requested here
typedef NodeList::iterator NodeItr;
^
/Developer/Extras/llvm/include/llvm/CodeGen/PBQP/Graph.h:31:11: note: forward declaration of 'PBQP::Graph::NodeEntry'
class NodeEntry;
据我所知,编译器会尝试实例化std::list<NodeEntry>
以获取迭代器。这失败了,因为尚未定义 NodeEntry。当然,EdgeEntry 正在使用 NodeEntry,反之亦然。
显而易见的问题是:我该如何解决?
教育问题是:为什么编译器在定义类型时尝试实例化模板?它不应该等到我们对列表做点什么吗?
谢谢。