Article View: comp.std.c++
Article #1804Re: Template instantiation question
From: Tony Cook
Date: Tue, 22 Aug 1995 08:18
Date: Tue, 22 Aug 1995 08:18
64 lines
3083 bytes
3083 bytes
Steve Clamage (clamage@Eng) wrote:
: 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.
Can we instead _declare_ the specialization and not define it:
extern int Foo<complex>::compare(const complex &);
So that we see an error at link time rather than run-time?
This may also need specialisations for any other function that call
compare - but these may be desirable anyway.
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.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:
<199508220325.NAA11355@online.tmx.com.au>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<41b09t$ltj@engnews2.Eng.Sun.COM>Show Full Headers
Complete Article Headers
Approved: fjh@cs.mu.oz.au Date: 22 Aug 95 08:18:17 GMT From: tony@online.tmx.com.au (Tony Cook) Message-ID: <199508220325.NAA11355@online.tmx.com.au> Newsgroups: comp.std.c++ Organization: Home Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back References: <41b09t$ltj@engnews2.Eng.Sun.COM> Return-Path: <daemon@ncar.UCAR.EDU> Subject: Re: Template instantiation question X-Newsreader: TIN [version 1.2 PL2] X-Original-Date: Tue, 22 Aug 1995 13:25:14 +1000