user5110759
user5110759

Reputation:

Using a template instance type within a template

If I were to write a template

  template<class T>
class myTemplate{
...
};

is there a syntax in either C++11 or C++14 which would allow me to identify a type of myTemplate<T>* within the actual template declaration?

For instance, what is the correct syntax for writing something like

template <class T>
class myTemplate{
...
void myTemplate(myTemplate<T>*, int);
....
};

TIA

Vinod

Upvotes: 0

Views: 57

Answers (1)

max66
max66

Reputation: 66200

Inside myTemplate<T> you can refer the same type (as method argument) as myTemplate<T> or simply as myTemplate. This is true also referring pointers to this type.

So you can write

 myTemplate (myTemplate<T> *, int) {}

but also

 myTemplate (myTemplate *, int) {}

When you want refer to myTemplate<T> for different T types, you have to explicit it. So you can write a template myTemplate constructor that accept a myTemplate pointer, for all possible myTemplate types, as follows

template <typename U>
myTemplate (myTemplate<U> *, int) { }

Obviously you have to avoid the identifier T for the internal template to avoid confusion with the external one.

Upvotes: 1

Related Questions