Topic: Can we refer to A<T, S> when we are constructing A<S, T>? A challenge!


Author: xjzhu@math.uwaterloo.ca (Xiaojun Zhu)
Date: Tue, 24 Nov 1992 02:45:17 GMT
Raw View
I have an interesting question for you C++ experts out there.

Q: Can we refer to A<T, S> when we are constructing A<S, T>?

I had experimented it with AT&T C++ V3.0 and the fun part is probably not
to give you the answer.

Please answer the following questions before resorting to your compiler.
(out of your knowledge)

Q1: Should we be allowed to use or not to use?

Q2: Why or why not?

template<class S, class T>
class A
{
  S _s;
  T _t;
public:
 //...
 // the following function is trying to switch a._s and a._t
 // to give me an A<T, S> object.
 friend A<T,S>& SWITCH(A<S,T>& a);  // interesting place

};


--------------------------------------------------------------
   A template version of my signature class is under repair.
Symptom: It dies without a warning if I use certain class
         as an argument.

                                       xjzhu@math.uwaterloo.ca
--------------------------------------------------------------




Author: kriss@mi.ibp.fr (Christophe GROSJEAN)
Date: Fri, 4 Dec 1992 09:06:20 GMT
Raw View
In article <By78zI.D8L@math.uwaterloo.ca> xjzhu@math.uwaterloo.ca (Xiaojun Zhu) writes:

>   I have an interesting question for you C++ experts out there.
>
>   Q: Can we refer to A<T, S> when we are constructing A<S, T>?
>
>   Please answer the following questions before resorting to your compiler.
>   (out of your knowledge)
>
>   Q1: Should we be allowed to use or not to use?
>
>   Q2: Why or why not?
>
>   template<class S, class T>
>   class A
>   {
>  S _s;
>  T _t;
>   public:
> //...
> // the following function is trying to switch a._s and a._t
> // to give me an A<T, S> object.
> friend A<T,S>& SWITCH(A<S,T>& a);  // interesting place
>
>   };

A1 : I think it should be allowed.

A2 : That's nothing more than a reference to another class,
because templates are supposed to define a different class
for each parameters.
As i understand it, templates are kind of macro definition
with the difference that parameters types are checked and
definitions can be distributed in source file (that is
methods don't need to be defined inside class).

I can't see why switching templates parametrers should be
more difficult than recursive definition of templates.

That's my guess, but i'll certainly try it...