template default

Hello,
i wanna know if is possible declare a class(A for ex) and use it with template default on your own.

1
2
template <class T, class U = A>
class A { .... }


If it`s possible, how can I do that ?
thank`s
I don't see why not, so long as the A has been forward declared. Obviously, you couldn't make an actual instance of A inside of A, but you could make a pointer or something like that.
I got errors doing this.
Can you do an example and post here?
You'd have to give it the first template parameter like this:
1
2
template<typename T, typename U = A<int> >
class A { /* * */ };
If I try this:
1
2
3
4
5
6
7
8
9
10
template<typename T, typename U = A<T> >
class A { 
	A<T> *ex;
};

int main(void){
	
	A<int> a;
	
};


and I get this:

1
2
3
4
5
6
7
8
teste.cpp:8:35: error: expected type-specifier before 'A'
teste.cpp:8:35: error: expected '>' before 'A'
teste.cpp:10:5: error: template argument 2 is invalid
teste.cpp: In function 'int main()':
teste.cpp:15:7: error: template argument 2 is invalid
teste.cpp:15:10: error: invalid type in declaration before ';' token
teste.cpp:15:10: warning: unused variable 'a'
Did you try forward declaring your class? A might not yet exist inside the template<> specifications for it.
What is the best way for forward declaring?

I did this:
1
2
template<typename T, typename U>
class A;

But I don't know how to use a prototype in this case.
Thanks.
Last edited on
That looks like a fine forward declaration to me.
What are you trying to do exactly?
Forward declaration or not, the problem here is the infinite template recursion.
You cant stop it by specifying a second template argument like this:
1
2
3
4
5
6
7
8
9
10
template<typename T, typename U = A<T, int> >
class A { 
	A<T> *ex;
};

int main(void){
	
	A<int> a;
	
};
closed account (zb0S216C)
+ 1 to Aquaz.

Wazzak
So, not is possible I do something like this:
1
2
3
4
5
6
7
8
9
10
template<typename T, typename U = A<T, A> >
class A { 
	A<T> *ex;
};

int main(void){
	
	A<int> a;
	
};

Because the template U must to be A and not int
Thank you
Topic archived. No new replies allowed.