Topic: When will member function templates be generated?
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sat, 29 Jan 1994 15:24:31 GMT Raw View
In article <27541@alice.att.com> bs@alice.att.com (Bjarne Stroustrup) writes:
>
>
>jjb@watson.ibm.com (John Barton @ IBM T.J. Watson Research Center) writes
>
> > Question: Must a member function template definition be legal
> > for every actual parameter the class template is expanded for,
> > even if the member function is never called?
> >
> > To say the same thing in another way, does the instantation of
> > a class template imply the instantation of each of the class'
> > member function template definitions?
>
>No. An implementation may only instantiate a function only if it has
>been used (called, had its address taken, etc.).
Unless its virtual. In which case, the implementation may
issue a diagnostic but doesnt have to (unless the virtual is called).
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd, CSERVE:10236.1703
6 MacKay St ASHFIELD, Mem: SA IT/9/22,SC22/WG21
NSW 2131, AUSTRALIA
Author: jhs@edg.com (John H. Spicer)
Date: Tue, 25 Jan 1994 16:02:40 GMT Raw View
In article <27541@alice.att.com> bs@alice.att.com (Bjarne Stroustrup) writes:
>
>
>jjb@watson.ibm.com (John Barton @ IBM T.J. Watson Research Center) writes
>
> > Question: Must a member function template definition be legal
> > for every actual parameter the class template is expanded for,
> > even if the member function is never called?
> >
> > To say the same thing in another way, does the instantation of
> > a class template imply the instantation of each of the class'
> > member function template definitions?
>
>No. An implementation may only instantiate a function only if it has
>been used (called, had its address taken, etc.).
Note that if a function is virtual, then it falls into the "etc" category
and it may also be instantiated.
John Spicer
Author: bs@alice.att.com (Bjarne Stroustrup)
Date: 17 Jan 94 14:18:31 GMT Raw View
jjb@watson.ibm.com (John Barton @ IBM T.J. Watson Research Center) writes
> Question: Must a member function template definition be legal
> for every actual parameter the class template is expanded for,
> even if the member function is never called?
>
> To say the same thing in another way, does the instantation of
> a class template imply the instantation of each of the class'
> member function template definitions?
No. An implementation may only instantiate a function only if it has
been used (called, had its address taken, etc.).
Author: jjb@watson.ibm.com (John Barton)
Date: Sun, 16 Jan 1994 22:38:10 GMT Raw View
Question: Must a member function template definition be legal
for every actual parameter the class template is expanded for,
even if the member function is never called?
To say the same thing in another way, does the instantation of
a class template imply the instantation of each of the class'
member function template definitions?
My vote is NO, only function template definitions actually called by a
program must be legal. Without this rule, a derived class cannot
override and redefine a base class member function when the base class
is a template and the base class member function fails to compile for
the template argument.
In the following example, a templatized base class has a member
function definition that will not be legal when expanded over the
template argument supplied by the derived class. The derived class
never calls the base class function (the copy constructor for
Pointer<T>). No other code calls that function either.
class Abstract {
public:
virtual void foo() = 0;
};
template<class T>
class Pointer {
public:
Pointer(const Pointer<T>& c);
Pointer();
protected:
T* p;
};
// The following template should never be expanded for
// T = Abstract.
template<class T>
Pointer<T>::Pointer(const Pointer<T>& c) { // out-line
p = new T(c); // not legal for Abstract.
}
template<class T>
class DerivedPointer :
public Pointer<T> {
public:
DerivedPointer(const DerivedPointer<T>& c)
// Calls base default ctor; Does NOT call base copy ctor.
{
p = 0;
}
DerivedPointer();
private:
T* p;
};
int main() {
DerivedPointer<Abstract> d; // Will Pointer<Abstract> copy ctor be generated?
DerivedPointer<Abstract> d2(d); // Copy ctor for DerivedPointer called.
}
--
John.
John J. Barton jjb@watson.ibm.com (914)784-6645
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598
Author: dag@control.lth.se (Dag Bruck)
Date: 17 Jan 1994 06:41:17 GMT Raw View
In <comp.std.c++> jjb@watson.ibm.com (John Barton) writes:
>Question: Must a member function template definition be legal
>for every actual parameter the class template is expanded for,
>even if the member function is never called?
No.
>To say the same thing in another way, does the instantation of
>a class template imply the instantation of each of the class'
>member function template definitions?
No.
-- Dag
Author: grumpy@cbnewse.cb.att.com (Paul J Lucas)
Date: 17 Jan 94 16:12:55 GMT Raw View