8
以下是合法吗?类别(或结构)通过模板自我引用
template< typename T >
struct tree_node
{
T t;
std::vector<tree_node> children;
};
对this post的评论似乎表明它不是。
编辑:这不会引起我作为“未定义的行为”类型的情况。预期的语义是明确的。如果它是一个不完整类型的无效使用,那么它应该是编译时错误。
In my tests这似乎工作正常(我已经使用GCC和Clang - 均与-Wall -Werror -std=c++11
)。
在语言定义(C++ 17之前)中是否有某些东西直接或间接地将其指定为未定义的行为,还是只是未指定?
请记住,这是非常相似的,在结构上,以类似下面:
typedef int T;
struct tree_node;
struct tree_node
{
T t;
tree_node * children;
}
可能dupliucate:http://stackoverflow.com/questions/31345193/how-can-an-incomplete-type-be-used-as-a-template-parameter-to-vector-here – NathanOliver
“例外shared_ptr“使我崩溃:https://stackoverflow.com/a/31347287/86967 – nobar
最糟糕的情况,你可以使用'shared_ptr'而不是'tree_node'。 –
nobar