Topic: My comments to the 3 questions on template.


Author: xjzhu@math.uwaterloo.ca (Xiaojun Zhu)
Date: Thu, 26 Nov 1992 16:00:43 GMT
Raw View
Concerning the 3 questions I had, I got a reply from Dr. Stroustrup.(See the
end of message)

First, let's look at question 3.

Question 3, Can A<S> and A<S, T> coexist?
-------------------------------------------

  I myself also found the answer for question 3 yesterday in ARM( Chapter 14,
Template, I believe) which states (not in the exact same wording) you can only
use "template-name" once in the same file scope.

 (for example, in the following declaration,

       template<class T> class A{/*...*/};

   A is called  the template-name, and the rule disallows us to use A
  as a template-name again in the same context)

Dr. Stroustrup basically gives us the same answer here.

My comment:

      I have no idea why there is such a restriction.     Several
   other replies I got are actually in favour of "yes" to this question.
   Well you can always argue that this will complicate the rule, make
   it more difficult to implement, but maybe not for this case.
   (Think about it.) I would think a "yes" to this question is natural
   since the real "template-class-name" is A<S> and A<S, T> respectively,
   and template class will be expanded only when there is an actual
   instance of the class needed to be declared, the notion of "template-name"
   should be of no interest to the compiler(or parser) anymore. Besides, even
   though the AT&T C++ V3.0 compiler got this rule correct(in the sense of
   conforming to ARM), it gives no interest message at all if I do have
   A<S> and A<S, T> coexist.( actually not a clue before you can pinpoint to
   the real problem.)

Now, let's have a look at question 2.

Question 2: Can we refer to A<T, S> when we are constructing A<S, T>?
---------------------------------------------------------------------

   I still didn't find the answer in those books I have(i.e., ARM, C++PL(2ed.),
C++ Primer), but we have the following answer from Dr. Stroustrup:

---> Yes, unless T is the same type as S A<S,T> is unrelated to<T,S>

My understand to the above answer is:

   (2.1). I can do the following:

   template<class S, class T>
   class A
   {
     //
   public:
     A<T, S>& SWITCH(A<S, T>&); // note here, we refer to A<T, S>
    //
   };

   (2.2). I can't do the follwoing:

    template<class S, class T>
    class A
    {
      //
    public:
     A<T, T>& BAD(A<T, T>&);  // nah, can't refer to A<T, T> in A<S, T>
    };

Comment: I am confused, how about you guys? Anyone has seen an implementation
         which satisfies the above two? (AT&T C++(V3.0) didn't conform to this.)

Finally, on question 1.

Question 1: Recusive template? are you sure?
---------------------------------------------

Comment: Dr. Stroustrup obviously gives to positive answers to this question,
         but I have yet to see a compiler which can do this "recursive" thing.
         (It should have the property that you don't have to instantiate every
          template got involved by hand, otherwise, what do you mean by
          "recursive"? You are allowed to instantiate the initial one of
          course.)

Sorry for this long message. Thank you very much for your attenstion.

Best wishes, Xiaojun Zhu

(Criticism about my impatience are accepted and efforts will be given to
overcome such shortcoming, well, as you know, it takes time, ...)

P.S. message from Dr. Stroustrup:
-------------------------------------------------------------------------------
>>From: bs@alice.att.com (Bjarne Stroustrup)
>>Newsgroups: comp.lang.c++,comp.std.c++
>>Subject: Re: Nobody is interested in questions on template? Why?
>>Keywords: questions, template, faq, book
>>References: <ByA29v.509@math.uwaterloo.ca>
>>Organization: AT&T Bell Laboratories, Murray Hill NJ
>>
>>1. Recursive template? Are you sure?
>>---> yes, you can have recursive templates. Yes, I'm sure.
>>
>>2. Can we refer to A<T, S> when we are constructing A<S, T>?
>>---> Yes, unless T is the same type as S A<S,T> is unrelated to<T,S>
>>
>>3. Can A<S> and A<S, T> coexist?
>>---> No, you can't overload class template names.