Topic: Can template arguments be a base class?


Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: Fri, 10 Jun 1994 15:05:46 GMT
Raw View
jgalt@leps5.phys.psu.edu (James M Dailey) writes:

>Can anyone tell me if the following is legal?
>
>template<class Type> class Envelope : public Type {
>
>...
>
>};

Yes, it's legal.

>My compiler (Symantec C++ for Mac v7.0) says "NO".

Your compiler is broken.

>The ARM doesn't say anything about it.  Thanx in advance.

Sure it does.  Try looking in the index under "templates" and
"inheritance".  In general if you can use feature A and you can use
feature B then you can use feature A and B together - unless explicitly
stated otherwise.

--
Fergus Henderson - fjh@munta.cs.mu.oz.au




Author: kinnucan@hq.ileaf.com (Paul Kinnucan)
Date: Fri, 10 Jun 1994 20:01:48 GMT
Raw View
In article <2t3qkc$2nc@entropy.sfe.com.au> andrewd@sfe.com.au (Andrew Davison) writes:

   jgalt@leps5.phys.psu.edu (James M Dailey) writes:


   >Can anyone tell me if the following is legal?

   >template<class Type> class Envelope : public Type {

   >...

   >};

   >My compiler (Symantec C++ for Mac v7.0) says "NO".  HOwever, I found this
   >in a library that was compiled with Borland C++ v3.1, so I am at a loss.
   >The ARM doesn't say anything about it.  Thanx in advance.

   Yes, it is legal, but you'd need to be a lawyer to pick that up from
   the ARM. You can find examples of usage in Stroustrup "C++ The
   Programming Language" (soory, no refs as I don't have it with me).

   Many compilers however still don't support it. BC++ and G++ do, Cfront
   (from Sun) didn't the last time I looked.

The double-ended list class templates in Borland's container library
actually make use of a template variable as a base class.

- Paul








Author: <MADXJL@rohvm1.rohmhaas.com>
Date: Wed, 8 Jun 1994 09:20:17 EDT
Raw View
>>
>>Can anyone tell me if the following is legal?
>>
>>template<class Type> class Envelope : public Type {
>>
>>...
>>
>>};
>>
>>My compiler (Symantec C++ for Mac v7.0) says "NO".  HOwever, I found this
>>in a library that was compiled with Borland C++ v3.1, so I am at a loss
>>The ARM doesn't say anything about it.  Thanx in advance.
>>

>
>        Yes, it's legal. As I recall, cfront says "sorry, not implemented"
>for this.
>        -- Pete

Anyone know if this is implemented in Turbo C++ 3.0 ?? This is an
AT&T 2.1 compliant compiler - is the above-described template feature
perhaps introduced in AT&T 3.0 ?? Turbo C++ 3.0 give an error
'type name expected' with the cursor pointing at the 'public:'

                              right here:
                                        |
  template<class Type> class Envelope : public Type {

 ......................................................
 : John Lynn - Rohm and Haas Company - (215) 592-2378 :
 '..........Internet: madxjl@rohvm1.rohmhaas.com .....'
  The opinions expressed are those of the writer, and
             not of Rohm and Haas Company




Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 8 Jun 1994 12:00:02 -0500
Raw View
One thing to watch out for if you use a template argument as
a base class: If you wish to refer to base class members (or
functions) within a derived template class, it is advisable
to explicitly qualify such names.

A recent clarification from the standard committee declares
that anything not dependent on a template argument uses
definitions available at the point the template is declared.

e.g.

int f(int);

template <class T> class X
{
  ...
  int g(int x) {return f(x);} // ::f(int) ALWAYS
};

The member function int X<T>::g(int) uses the global function
::f(int) EVEN if class T has a member function f(int). (This
example assumes that no member named f is declared within the
template.)




Author: huber@kazoo.cs.uiuc.edu (Jay Huber)
Date: 8 Jun 1994 18:26:10 GMT
Raw View
In article <2t4tei$6gv@fsgi01.fnal.gov>, b91926@fsgi01.fnal.gov (David
Sachs) writes:
|> One thing to watch out for if you use a template argument as
|> a base class: If you wish to refer to base class members (or
|> functions) within a derived template class, it is advisable
|> to explicitly qualify such names.
|>
|> A recent clarification from the standard committee declares
|> that anything not dependent on a template argument uses
|> definitions available at the point the template is declared.
|>
|> e.g.
|>
|> int f(int);
|>
|> template <class T> class X
|> {
|>   ...
|>   int g(int x) {return f(x);} // ::f(int) ALWAYS
|> };
|>
|> The member function int X<T>::g(int) uses the global function
|> ::f(int) EVEN if class T has a member function f(int). (This
|> example assumes that no member named f is declared within the
|> template.)

But in this example, there is no derived class.  Why would anyone
expect T's scope to be used automatically when T is not derived from?
Of course you need T::f(x) -- that's obvious.  On the other hand,
if you actually derive from T, then you have a different situation...

  int f(int) { return 0; }

  template <class T>
  class X : public T {
    int g(int x) { return f(x); }  // calls ::f(x) unless T defines f(x)
    };

  class foo {
  public:
    int f(int x) { return x+1; }
    };

  int main() {
    X<foo> x;
    cout << x.g(1) << endl;    // should print 2
    return 0;
    }

Has this behavior been changed?  I hope not.  That would mean you would
have to be more explicit:

  template <class T>
  class X : public T {
    int g1(int x) { return T::f(x); }
    int g2(int x) { return this->f(x); }
    };

Which seems silly to me.


Clarifications anyone?




Author: andrewd@sfe.com.au (Andrew Davison)
Date: 8 Jun 1994 17:05:48 +1000
Raw View
jgalt@leps5.phys.psu.edu (James M Dailey) writes:


>Can anyone tell me if the following is legal?

>template<class Type> class Envelope : public Type {

>...

>};

>My compiler (Symantec C++ for Mac v7.0) says "NO".  HOwever, I found this
>in a library that was compiled with Borland C++ v3.1, so I am at a loss.
>The ARM doesn't say anything about it.  Thanx in advance.

Yes, it is legal, but you'd need to be a lawyer to pick that up from
the ARM. You can find examples of usage in Stroustrup "C++ The
Programming Language" (soory, no refs as I don't have it with me).

Many compilers however still don't support it. BC++ and G++ do, Cfront
(from Sun) didn't the last time I looked.

Regards.








Author: jgalt@leps5.phys.psu.edu (James M Dailey)
Date: 7 Jun 1994 16:44:32 -0400
Raw View
Can anyone tell me if the following is legal?

template<class Type> class Envelope : public Type {

...

};

My compiler (Symantec C++ for Mac v7.0) says "NO".  HOwever, I found this
in a library that was compiled with Borland C++ v3.1, so I am at a loss.
The ARM doesn't say anything about it.  Thanx in advance.

-James








Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Tue, 7 Jun 1994 22:01:53 GMT
Raw View
In article <2t2m7g$b5a@leps5.phys.psu.edu>,
James M Dailey <jgalt@leps5.phys.psu.edu> wrote:
>
>Can anyone tell me if the following is legal?
>
>template<class Type> class Envelope : public Type {
>
>...
>
>};
>
>My compiler (Symantec C++ for Mac v7.0) says "NO".  HOwever, I found this
>in a library that was compiled with Borland C++ v3.1, so I am at a loss.
>The ARM doesn't say anything about it.  Thanx in advance.
>

 Yes, it's legal. As I recall, cfront says "sorry, not implemented"
for this.
 -- Pete