Topic: template class member function instantiation after specialization


Author: chas <chas_honton@my-deja.com>
Date: 1999/10/31
Raw View
In article <3818BF24.AD837C4E@engr.sgi.com>,
  Branko Cibej <brane@cthulhu.engr.sgi.com> wrote:
<snip>
> > The automatic variable instance of Node<int>(1) on line 13 causes an
> > implicit instantiation of Node<int>.  However, does it cause an
> > implicit instantiation of Node<int>::~Node()?  The instantiation
needs
> > to occur, but where does the definition of the destructor come from?
>
> The destructor is needed when the variable `instance' goes out of
> scope (somewhere between the `;' in line 13 and the `}' in line 14
> :-), so it has to be instantiated there.

Yes, I know that.  The question was, Where does the definition of the
destructor come from?

chas


Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: chas honton <chas@secant.com>
Date: 1999/10/28
Raw View
I have compiled the following code with six different compilers:

--------------------------------
1  template <class T>
2  class Node
3  {
4    T key;
5  public:
6    Node(T _key ) : key(_key) { }
7    ~Node();
8  };
9
10 void fnc()
11 {
12   int x= 1;
13   Node<int> instance(1);
14 }
15
16 template <> Node<int> :: ~Node() {}
--------------------------------

4/6 offer no complaints, 1 warns about future error, and 1 states that
on line 16, template<>Node<int>::~Node() is a specialization after
instantiation.  Is this truly an error?  Let's go to the standard
(well, actually the draft because I had trouble downloading the
standard, sigh):

CD2 14.5.4 - 1: If a template is partially specialized then that
   partial specialization shall be declared before the first use of
   that partial specialization that would cause an implicit
   instantiation to take place

The automatic variable instance of Node<int>(1) on line 13 causes an
implicit instantiation of Node<int>.  However, does it cause an
implicit instantiation of Node<int>::~Node()?  The instantiation needs
to occur, but where does the definition of the destructor come from?
The standard speaks of the lack of the template definition as being
ill formed, but does not speak of missing template class method
definitions:

CD2 14.7.1 - 5: If an implicit instantiation of a class template
   specialization is required and the template is declared but not
   defined, the program is ill-formed.  [Example:
     template<class T> class X;
     X<char> ch; // error: definition of X required
   --end example]

Is the template declared but not defined?  No, the definition occurs
on lines 1-8.  Is the template class destructor (a template class
member function) declared but not defined?  Yes -- What does the
standard say about template class member functions declared but not
defined at the point of implicit instantiation?  Help!  I can't find
any relevant wording!


As to whether the original program is legal, I would go back to why
there is a specialization before instantiation requirement -- the
compiler can't undo a prior template instantiation and code generation
when it finds a specialization.  However, if code generation is just a
call and linkage directives, because I know what to call, but not how
it is defined, providing the method specialization definition after
the instantiation or in another translation unit becomes a link time
task.

There are a lot of uses for this partial template class definition by
frameworks.  The framework provides most of the code with templates
and the user is expected to provide specialization of particular
methods.  This is similar to overriding the pure virtual methods of a
base class implementation.

Chas Honton
Secant Technologies
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Branko Cibej <brane@cthulhu.engr.sgi.com>
Date: 1999/10/30
Raw View
chas honton wrote:
>
> I have compiled the following code with six different compilers:
>
> --------------------------------
> 1  template <class T>
> 2  class Node
> 3  {
> 4    T key;
> 5  public:
> 6    Node(T _key ) : key(_key) { }
> 7    ~Node();
> 8  };
> 9
> 10 void fnc()
> 11 {
> 12   int x= 1;
> 13   Node<int> instance(1);
> 14 }
> 15
> 16 template <> Node<int> :: ~Node() {}
> --------------------------------
>
> 4/6 offer no complaints, 1 warns about future error, and 1 states that
> on line 16, template<>Node<int>::~Node() is a specialization after
> instantiation.  Is this truly an error?  Let's go to the standard
> (well, actually the draft because I had trouble downloading the
> standard, sigh):
>
> CD2 14.5.4 - 1: If a template is partially specialized then that
>    partial specialization shall be declared before the first use of
>    that partial specialization that would cause an implicit
>    instantiation to take place
>
> The automatic variable instance of Node<int>(1) on line 13 causes an
> implicit instantiation of Node<int>.  However, does it cause an
> implicit instantiation of Node<int>::~Node()?  The instantiation needs
> to occur, but where does the definition of the destructor come from?

The destructor is needed when the variable `instance' goes out of
scope (somewhere between the `;' in line 13 and the `}' in line 14
:-), so it has to be instantiated there.

    Brane
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]