class (or struct) self-reference by template

只谈情不闲聊 提交于 2019-11-28 03:28:08

问题


Is the following legal?

template< typename T >
struct tree_node
   {
   T t;
   std::vector<tree_node> children;
   };

A comment to this post seems to suggest that it is not.


EDIT: This doesn't strike me as an "undefined behavior" type of scenario. The intended semantics are unambiguous. If it is an invalid usage of an incomplete type then it should be a compile-time error.

In my tests this seems to work fine (I have used both GCC and Clang -- both with -Wall -Werror -std=c++11).

Is there something in the language definition (prior to C++17) that directly or indirectly specifies this as undefined behavior, or is it just under-specified?


Keep in mind that this is very similar, structurally, to something like the following:

typedef int T;
struct tree_node;

struct tree_node
   {
   T t;
   tree_node * children;
   }

回答1:


Actually, as a result of N4371 we have (from N4527, [vector.overview], will be in C++17):

An incomplete type T may be used when instantiating vector if the allocator satisfies the allocator completeness requirements 17.6.3.5.1. T shall be complete before any member of the resulting specialization of vector is referenced.

Prior to this, vector could not be constructed with an incomplete type (which tree_node is at that point), and that would be undefined behavior.



来源:https://stackoverflow.com/questions/32487575/class-or-struct-self-reference-by-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!