Article View: comp.std.c++
Article #1856Re: Template instantiation question
From: Steve Clamage
Date: Mon, 28 Aug 1995 16:11
Date: Mon, 28 Aug 1995 16:11
63 lines
3287 bytes
3287 bytes
Distribution:
ncm@netcom.com (Nathan Myers) writes:
>clamage@Eng (Steve Clamage) writes:
>>tony@online.tmx.com.au (Tony Cook) writes:
>>>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?
>>
>>No. The declaration does not prevent instantiation if any use is made
>>of the function. That is, the declaration tells the compiler that
>>particular compare function is supposed to exist. If it is needed
>>but not found at link time, the system will generate it from the
>>template definition.
>(I'm sorry to jump in late.)
>Sorry, I don't think this is right. As I understand it, declaring
>a specialization before every place it is used *does* prevent
>automatic instantiation. It's a violation of ODR (not required
>to be detected) if you miss declaring it first, though, in one or
>more places.
It seems I had two similar things confused. Thanks for the correction.
According to the 28 April draft, if you declare a NON-template
function that matches something that could be generated from the
function template, it does not prevent the generation of the
template instantiation. Example from the draft:
template<class T> T max(T a, T b) { return a > b ? a : b; }
int max(int, int);
The declaration does not prevent generation of max from the template
for type int, which is what I was originally thinking of. The
declaration allows you to write
char c; int i;
...
max(c, i);
and get the effect of
max(int(c), i);
via ordinary overload resolution. Without the declaration, you could
not compare an char with an int without an explicit cast. In this
case you must not define the function max(int,int) because that
function will be generated from the template.
But if you declare an explicit template specialization
int max<int>(int, int);
that DOES prevent the specialization from being generated
automatically. The declaration is in effect a promise that you
will supply a specialization. As Nathan says, you must be careful
to provide this declaration every place the specialization is
needed to prevent the compiler from generating it.
So the answer to the original question is "Yes, that should work".
Technically, the program is ill-formed if the program calls an
undefined function, but realistically the program will just
fail to link -- which is what was wanted.
--
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:
<MATT.95Aug28091123@physics2.Berkeley.EDU>Path:
arc1-ca.pugleaf.net!128.32.155.1!mattReferences:
<41b09t$ltj@engnews2.Eng.Sun.COM> <199508220325.NAA11355@online.tmx.com.au> <41cvdk$4bp@engnews2.Eng.Sun.COM> <ncmDDzo9o.9Bx@netcom.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt From: clamage@Eng.Sun.COM (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Template instantiation question Date: 28 Aug 1995 16:11:22 GMT Organization: Sun Microsystems Inc., Mountain View, CA Approved: matt@physics2.Berkeley.EDU (Matt Austern) Message-ID: <MATT.95Aug28091123@physics2.Berkeley.EDU> References: <41b09t$ltj@engnews2.Eng.Sun.COM> <199508220325.NAA11355@online.tmx.com.au> <41cvdk$4bp@engnews2.Eng.Sun.COM> <ncmDDzo9o.9Bx@netcom.com> NNTP-Posting-Host: physics2.berkeley.edu