Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

c++ - class (or struct) self-reference by template

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;
   }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...