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