Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
   }
like image 627
Brent Bradburn Avatar asked Sep 09 '15 19:09

Brent Bradburn


1 Answers

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.

like image 163
Barry Avatar answered Oct 14 '22 03:10

Barry