Article View: comp.std.c++
Article #2350Re: Template Instantiation
From: David Vandevoorde
Date: Mon, 30 Oct 1995 02:49
Date: Mon, 30 Oct 1995 02:49
88 lines
3643 bytes
3643 bytes
>>>>> "MR" == Michael Rice <mpr@absoft.com> writes:
MR> I have a question concerning class template instantitation, as described
MR> in section 14.3.2 on the Sep 1995 WP.
MR> Paragraph 6 says:
MR> An implementation shall not instantiate a function, nonvirtual member
MR> function, class or member class that does not require instantiation.
MR> The section goes on the give examples of a few cases that do require
MR> instantiation and a few that do not require instantiation. It seems
MR> to me, that it should state EXACTLY when they are required and it
MR> doesn't seem to in this section. Does it anywhere? If it is not
I believe it is implicit in the semantic descriptions throughout the DWP.
For functions (member or not), they should only be instantiated if they
are called. The only exception are virtual functions: they can be
instantiated if the class to which they belong is instantiated.
Classes and member classes should be instantiated only if their size
or at least part of their layout is needed. If only their name is needed,
no instantiation should occur.
MR> stated implementations could decide anything in a requirement. Couldn't
MR> they? Take this example:
MR> struct incomplete;
MR> template<class T>
MR> struct reverse_iterator {
MR> reverse_iterator ( );
MR> incomplete i;
MR> };
MR> typedef reverse_iterator<long> reverse_iterator1 ;
MR> template < class T >
MR> struct vector {
MR> vector ( );
MR> };
MR> main()
MR> {
MR> vector < int > i ;
MR> return 0;
MR> }
MR> If an implementation decides the typedef above makes the instantiation
MR> of reverse_iterator<long> required this code is ill-formed as the struct
MR> incomplete is incomplete.
MR> On the other hand, the implementation could say it is not required due to
MR> this typedef (which seems correct to me.) Now the code is well formed?
Indeed, only the ``name'' of the type is needed: that does not require
instantiation. The fact that some compilers (unconformingly) do instantiate
in this case, and that this was a fairly popular method of explicitly
invoking instantiation is somewhat misleading.
MR> What about this:
MR> template<class T>
MR> struct reverse_iterator {
MR> reverse_iterator ( );
MR> };
MR> typedef reverse_iterator<long> reverse_iterator1 ;
MR> template < class T >
MR> struct vector {
MR> reverse_iterator1 rbegin ( ) { return reverse_iterator1 ( ) ; }
MR> vector ( );
MR> };
MR> main()
MR> {
MR> vector < int > i ;
MR> return 0;
MR> }
MR> Is reverse_iterator<long> required to be instantiated here even though
MR> the inline function is never called?
[...]
No, it should not be instantiated (unless it were virtual). Many compilers
are sloppy about this, unfortunately.
Daveed
---
[ 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:
<VANDEVOD.95Oct29114646@avs.cs.rpi.edu>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<9510272025.AA09665@absoft.absoft.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 30 Oct 95 02:49:11 GMT Approved: fjh@cs.mu.oz.au From: vandevod@cs.rpi.edu (David Vandevoorde) Newsgroups: comp.std.c++ Subject: Re: Template Instantiation X-Original-Date: 29 Oct 1995 16:46:46 GMT Organization: RPI Computer Science Message-ID: <VANDEVOD.95Oct29114646@avs.cs.rpi.edu> References: <9510272025.AA09665@absoft.absoft.com> In-Reply-To: "Michael Rice"'s message of 29 Oct 95 04:14:28 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMJQ9PeEDnX0m9pzZAQHaGQF/e8YnXSmCSe9ujmrRGRGfSNKF73zN/qUH 1mC2J6xIwNPekekEU/NnMijcZA8Toev8 =fdGc