Weird template syntax

Hi, this is the first time I see something like this. I had this definition of class and I had to finish it

1
2
3
4
5
6
7
8
9
10
11
 template<class T>
class Ptr {
	T* ptr;
	T* array;
	int sz;
public:
	template<int N>
	Ptr(T* p, T(&a)[N]);	//bind to array a, sz == N, initial value p
	
        // . . . 
};

this part really confuses me
1
2
	template<int N>
	Ptr(T* p, T(&a)[N]);

Visual studio generated code for definition of this constructor and its like this
1
2
3
4
5
template<class T>
template<int N>
Ptr<T>::Ptr(T * p, T(&a)[N])
{
}

There are 2 template<...>in a row. I understand If it were something like
template<class A, class B> but not
template<class A> template<class B>

Given that I created this constructor like this
1
2
3
template<class T>
template<int N>
Ptr<T>::Ptr(T * p, T(&a)[N]) : ptr{ p }, array{a}, sz { N } {}

how do you even create Ptr object

1
2
3
4
5
6
7
8
int main(){

int arr[10];

Ptr<int> p{arr, arr<10>}; //like this(obviously failed) ?
Ptr<int, int> p{arr, 10}; //like this(obviously failed) ?

}
I don't understanding the purpose of this class or function but I can try to explain the syntax.

The first template<class T> is for the class template Ptr.

The second template<int N> is for the function template Ptr<T>::Ptr.

I don't think it is possible to explicitly pass template arguments to the constructor but you probably don't need to do that anyway. Just pass the array as argument to the constructor and the template argument N will be deduced automatically.

 
Ptr<int> p{arr, arr};
Last edited on
Cool, tnx you very much! The class is supposed to keep track on what element pointer points to and throw an exception in case it doesn't point to any of this arrays elements anymore and is being dereferenced.
Last edited on
Topic archived. No new replies allowed.