Recursive container type for container / name forwarding

Is there a way to get a recursive template type? I have a container for which I want to specify a basic storage strategy. However, the internal template must use the external type of the template, so it invokes a loop in the definition of a type that cannot be specified.

About what I want:

template<typename C> struct inner { C * object[16]; }; template<typename T, typename Inner> struct container { T value; Inner<container> holder; }; 

The C ++ 11 solutions are great (although I'm still on gcc 4.6.3).

+6
source share
2 answers

You need to tell the compiler that Inner is a template:

 template<typename T, template<typename> class Inner> struct container { T value; Inner<container> holder; }; 
+5
source

I'm not sure why you are adding a template parameter of type Inner , since you define holder as a type based on Container and Inner , both of which are available to indicate that you are declaring a holder.

Do you plan to use any type other than struct inner as a template parameter for Container ? If not, the following simplified code is compiled and launched for me in VS2010:

 #include <vector> #include <stdio.h> template <typename C> struct inner{ C * objects[16]; bool hasobj; inner():hasobj(false){} }; template <typename T> class Container { inner<Container> holder; T value; public: Container(const T& valueP){ value = valueP; } void AddChild(Container* rhs){ holder.objects[0] = rhs; //Always using first location, just for example holder.hasobj = true; } void PrintStuff()const{ if(holder.hasobj){ holder.objects[0]->PrintStuff(); } printf("VAL IS %d\n", value); } }; int main(){ Container<int> c(10); Container<int> c1(20); c1.AddChild(&c); c1.PrintStuff(); } 

In principle, it is assumed that the Container always determines the holder in terms of Inner , which helps get rid of the additional template parameter in determining the Container . Hope this helps :) Arun

0
source

Source: https://habr.com/ru/post/950622/


All Articles