对于我的一个项目,我真正想做的是(将其简化到最低限度);
struct Move
{
int src;
int dst;
};
struct MoveTree
{
Move move;
std::vector<MoveTree> variation;
};
我必须承认,我认为不可能直接执行此操作,我认为 MoveTree 中的 MoveTree vector 将被禁止。但无论如何我都试过了,效果很好。我正在使用 Microsoft Visual Studio 2010 Express。
这是便携的吗?这是好习惯吗?我有什么需要担心的吗?
编辑:我问了第二个 question希望能找到一个好的方法。
最佳答案
C++ 标准 (2003) 明确指出,用不完整类型实例化标准容器会调用未定义行为。
规范在 §17.4.3.6/2 中说,
In particular, the effects are undefined in the following cases:
__ [..]
— if an incomplete type (3.9) is used as a template argument when instantiating a template component.
__ [..]
随着 C++17 标准的改变,事情发生了变化,它明确地允许 std::list
、std::vector
和 std 的这种类型的递归::forward_list
。如需引用,请参阅 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4510.html这个答案:How can I declare a member vector of the same class?
关于c++ - C++ 递归类型定义是否可能,特别是我可以将 vector<T> 放在 T 的定义中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6517231/