🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1782

Re: Template instantiation question

#1782
From: Steve Clamage
Date: Mon, 21 Aug 1995 22:11
54 lines
2658 bytes
In article 95Aug21100336@physics10.Berkeley.EDU, bmcphers@alias.com (Brent McPherson) writes:
>
>I have seen template classes that define methods that will only *work*
>if the instantiation type provides certain methods/functionality.
>
>ie.
>
>  template<class T>
>  class Foo {
>  public:
>     Foo (const T &t) : myT(T) {}
>     // etc...
>     int compare (const T &otherT) 
>     { 
>       return (myT < otherT ? -1 : myT > otherT ? 1 : 0);
>     }
>     // etc...
>  };
>
>In this example the "compare" method relies on the object of type T
>having comparison operators.
>
>Is it legal to allow Foo to be instantiated with an object that does
>not support comparison as long as the compare method is never called
>for that particular instantiation?

The draft standard, 14.3.2 Point of Instantiation [temp.point], says
the implementation shall not instantiate a class or function that does not
require instantiation. A non-virtual function that is never called and
never has its address taken does not require instantiation.

In your example, if you create a variable of type Foo<X>, where X is
some type, and never use the non-virtual compare function, that
function, Foo<X>::compare, should not be instantiated.

Details of template definition and instantiation have been modified or
clarified recently, and it will take a while for compilers to catch up
to what is in the draft standard. 

In the mean time, you could write a specialization of Foo<X>::compare
that throws an exception or invokes "assert(0)" to allow your code
to compile and link. That is, for example, if you use Foo<complex>,
you could provide the specialization
	int Foo<complex>::compare(const complex&) { assert(0); return 0; }
which will prevent the compiler from generating a version from the template,
and will thus allow your program to compile and link.
---
Steve Clamage, stephen.clamage@eng.sun.com



[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <41b09t$ltj@engnews2.Eng.Sun.COM>
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage
From: clamage@Eng (Steve Clamage)
Newsgroups: comp.std.c++
Subject: Re: Template instantiation question
Date: 21 Aug 1995 22:11:09 GMT
Organization: Sun Microsystems Inc., Mountain View, CA
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <41b09t$ltj@engnews2.Eng.Sun.COM>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Type: text
X-Nntp-Posting-Host: taumet.eng.sun.com
Content-Length: 1890
X-Lines: 50
Originator: clamage@taumet