Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C++ recursive type definitions possible, in particular can I put a vector<T> within the definition of T?

Tags:

c++

For one of my projects, what I really wanted to do was this (simplifying it to the bare minimum);

struct Move {     int src;     int dst; };  struct MoveTree {     Move move;     std::vector<MoveTree> variation; }; 

I must admit that I assumed that it wouldn't be possible to do this directly, I thought a vector of MoveTree s within a MoveTree would be verboten. But I tried it anyway, and it works beautifully. I am using Microsoft Visual Studio 2010 Express.

Is this portable ? Is it good practise ? Do I have anything to worry about ?

Edit: I've asked a second question hoping to find a good way of doing this.

like image 240
Bill Forster Avatar asked Jun 29 '11 07:06

Bill Forster


2 Answers

The C++ Standard (2003) clearly says that instantiating a standard container with an incomplete type invokes undefined-behavior.

The spec says in §17.4.3.6/2,

In particular, the effects are undefined in the following cases:

__ [..]
— if an incomplete type (3.9) is used as a template argument when instantiating a template component.
__ [..]

Things have changed with the C++17 standard, which explicitely allows this types of recursion for std::list, std::vector and std::forward_list. For reference, see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4510.html and this answer: How can I declare a member vector of the same class?

like image 185
Nawaz Avatar answered Sep 22 '22 14:09

Nawaz


MoveTree is an incomplete type inside its definition. The standard does not guarantee instantiation of STL templates with incomplete types.

like image 40
mmx Avatar answered Sep 20 '22 14:09

mmx