recursion - Recursive containers in C++? -


possible duplicate:
are c++ recursive type definitions possible, in particular can put vector<t> within definition of t ?

i looking through code , noticed data structure similar following:

class treenode {     std::vector<treenode> subnodes; }; 

as can see, container instantiated treenode before treenode has been defined. code compiles under both gcc , msvc, remember seeing saying not guaranteed behaviour. unfortunately can't find in standard discussing @ all.

how such containers able implemented? behaviour guaranteed standard? if not guaranteed standard, alternatives have design?

this fine because std::vector<t> class doesn't contain concrete instances of type t in it: it's typically implemented using pointers. template instantiation std::vector<treenode> not require full definition of treenode.

std::vector<t> implemented triplet of pointers (though not required standard):

template <typename t> class vector {   ...   t* start;   t* end;   t* end_of_storage; }; 

if std::vector<t> did contain concrete instances of t in it, have problem. following not legal c++, because creates circular "has a" definition:

template <typename t> class container {   t x; };  class treenode {   container<treenode> subnodes; }; 

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -