Topic: templates and functions not called


Author: Michael Cook <mcook@cognex.com>
Date: 06 Feb 1995 15:45:49 GMT
Raw View
In D&E $15.5 (bottom of p.347), <bs> says "I consider it essential to allow an
implementation to instantiate only the template functions actually used."

Is this still the current thinking?

In particular, I'm wondering...if I have a class template that defines X(X&)
and operator=(X&), and the template assumes that the parameter class T has
T(T&) and operator=(T&), should I be able to instantiate X for a class T that
has no T(T&) or operator=(T&) provided I never try to invoke X(X&) or
operator=(X&)?

Gcc seems to say "no", but D&E seems to say "yes."
--
 Michael Cook <mcook@cognex.com>
 Telephone: +1 508 650 3251, Fax: +1 508 650 3336
 Cognex Corporation, One Vision Drive, Natick, Massachusetts 01760-2059




Author: Michael Cook <mcook@cognex.com>
Date: 06 Feb 1995 15:52:20 GMT
Raw View
In D&E $15.5 (bottom of p.347), <bs> says "I consider it essential to allow an
implementation to instantiate only the template functions actually used."

Is this still the current thinking?

In particular, I'm wondering...if I have a class template that defines X(X&)
and operator=(X&), and the template assumes that the parameter class T has
T(T&) and operator=(T&), should I be able to instantiate X for a class T that
has no T(T&) or operator=(T&) (that is, these member functions are private)
provided I never try to invoke X(X&) or operator=(X&)?

Gcc seems to say "no", but D&E seems to say "yes."
--
 Michael Cook <mcook@cognex.com>
 Telephone: +1 508 650 3251, Fax: +1 508 650 3336
 Cognex Corporation, One Vision Drive, Natick, Massachusetts 01760-2059




Author: jason@cygnus.com (Jason Merrill)
Date: Mon, 6 Feb 1995 21:00:44 GMT
Raw View
>>>>> Michael Cook <mcook@cognex.com> writes:

> In D&E $15.5 (bottom of p.347), <bs> says "I consider it essential to
> allow an implementation to instantiate only the template functions
> actually used."

> Is this still the current thinking?

Yes.

> In particular, I'm wondering...if I have a class template that defines
> X(X&) and operator=(X&), and the template assumes that the parameter
> class T has T(T&) and operator=(T&), should I be able to instantiate X
> for a class T that has no T(T&) or operator=(T&) provided I never try to
> invoke X(X&) or operator=(X&)?

Yes.

> Gcc seems to say "no", but D&E seems to say "yes."

This is a bug in g++.

Jason




Author: Greg Wilkins <gregw@ind.tansu.com.au>
Date: 7 Feb 1995 00:15:44 GMT
Raw View
Michael Cook <mcook@cognex.com> wrote:
>
> In D&E $15.5 (bottom of p.347), <bs> says "I consider it essential to allow an
> implementation to instantiate only the template functions actually used."
>
The problem with this approach is that with virtual functions, it is
not possible to work out the functions actually used (halting
problem, NP complete etc. etc.).

I think it is up to library/framework designers to come up with
narrow interfaces to template classes, with methods moved down to
non templated base classes.

------------------------------------------------------------------------------
Greg Wilkins:Consultant for Object Oriented Pty. Ltd. (OOPL)|You're not Dorothy
       Site @Telecom, Intelligent Network Development       |I'm not Toto!
       Snail:P.O. Box 1826,North Sydney,NSW.2089, Australia |And this
       Email:gregw@ind.tansu.com.au (gregw@oose.com.au)     |definitely is
       Fax  :(+61 2) 3953225 or OOPL Office:(+61 2) 9565089 |not Kansas!
       Phone:(+61 2) 3953461 or OOPL Office:(+61 2) 9571092 |  -Fleischman
-------------------------------------------------------------------------------





Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Tue, 7 Feb 1995 22:51:37 GMT
Raw View
In article <MCOOK.95Feb6104549@erawan.cognex.com> Michael Cook <mcook@cognex.com> writes:
>In D&E $15.5 (bottom of p.347), <bs> says "I consider it essential to allow an
>implementation to instantiate only the template functions actually used."
>
>Is this still the current thinking?

 Yes. It is considered mandatory. (Note that, of course,
a virtual function is "used" even if not called)

>In particular, I'm wondering...if I have a class template that defines X(X&)
>and operator=(X&), and the template assumes that the parameter class T has
>T(T&) and operator=(T&), should I be able to instantiate X for a class T that
>has no T(T&) or operator=(T&) provided I never try to invoke X(X&) or
>operator=(X&)?

 Yes.
>
>Gcc seems to say "no", but D&E seems to say "yes."

 Selective instantiation is considered mandatory, but
it will take some time before all compilers implement it.
However, be very careful with the copy constructor, it isn't
always clear when one is required to exist.

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189