Topic: Are virtual functions of a template class instantiated?


Author: alf_p_steinbach@yahoo.no.invalid (Alf P. Steinbach)
Date: Thu, 10 Oct 2002 20:49:09 +0000 (UTC)
Raw View
On Thu, 10 Oct 2002 19:52:44 +0000 (UTC), hyrosen@mail.com (Hyman Rosen) wrote:

>Alf P. Steinbach wrote:
>
>> *which leaves it open what "does not require specialization" means*
>
>It's not open, it's covered by 14.7.1/1.
>
>> *and then =A714.7.1/10*
>
>That paragraph does not say what you claim it does.
>Do you have an actual copy of the standard?

As I wrote,


   <quote>
   The closest I find, using the CD2, ...
   </quote>


where, of course, "CD2" refers to the second committee draft.

Does that answer the question?



>Instead, 14.6.4.1/4 says
>
>     If a virtual function is implicitly instantiated,
>     its point of instantiation is immediately following
>     the point of instantiation of its enclosing class
>     template specialization.
>
>When we finally implement virtual member template methods,
>we're going to need runtime template instantiation and
>compilation, and the standard had the foresight to allow
>implementations to use the same method to delay virtual
>method instantiation :-)

Heh heh... :-)

As you know, I disagree with you on that.

Cheers,

- Alf

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Nicola Musatti <Nicola.Musatti@ObjectWay.it>
Date: Fri, 11 Oct 2002 12:24:55 CST
Raw View
[posted only to c.s.c++, as I don't subscribe to c.l.c++]

Alf P. Steinbach wrote:

> On Thu, 10 Oct 2002 19:52:44 +0000 (UTC), hyrosen@mail.com (Hyman Rosen) wrote:
[...]
>>That paragraph does not say what you claim it does.
>>Do you have an actual copy of the standard?
>
> As I wrote,
>
>    <quote>
>    The closest I find, using the CD2, ...
>    </quote>
>
> where, of course, "CD2" refers to the second committee draft.

Please do not quote an unfinished, non normative, five year old
document, especially in this newsgroup. This thread shows how this can
lead to misunderstanding.

Thank you,
Nicola Musatti

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: SiemelNaran@KILL.att.net ("Siemel Naran")
Date: Thu, 10 Oct 2002 18:04:58 +0000 (UTC)
Raw View
Are virtual functions of a template class instantiated?


class B1 {
public:
   virtual ~B1() { }
   virtual void f() { }
};

template <class B>
class D : public B {
public:
   virtual void f() { B::f(); }
}

class D1 : public D<B1> { };


In the code above we do not call D<B>::f() directly.  Were it a non-virtual
function, the compiler would be required to not instantiate it.  But if it
is a virtual function, is the compiler required to instantiate it?


Reason is that main() I do stuff like

int main() {
   B1 * b1 = new D1();
   b1->f(); // should call D<B1>::f()
   delete b1;
}



--
+++++++++++
Siemel Naran

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: alf_p_steinbach@yahoo.no.invalid (Alf P. Steinbach)
Date: Thu, 10 Oct 2002 18:46:10 +0000 (UTC)
Raw View
On Thu, 10 Oct 2002 18:04:58 +0000 (UTC), SiemelNaran@KILL.att.net ("Siemel Naran")
wrote:

>Are virtual functions of a template class instantiated?
>
>
>class B1 {
>public:
>   virtual ~B1() { }
>   virtual void f() { }
>};
>
>template <class B>
>class D : public B {
>public:
>   virtual void f() { B::f(); }
>}
>
>class D1 : public D<B1> { };
>
>
>In the code above we do not call D<B>::f() directly.  Were it a non-virtual
>function, the compiler would be required to not instantiate it.  But if it
>is a virtual function, is the compiler required to instantiate it?

Yes, in practice, otherwise the code wouldn't work.

Theory (what the standard says) is something else.

The closest I find, using the CD2, is    14.7.1/7,


    An implementation shall not implicitly instantiate a function,
    nonvirtual member function, class or member template that does not
    require instantiation. It is unspecified whether or not an
    implementation implicitly instantiates a virtual member function
    that does not require specialization.


which leaves it open what "does not require specialization" means, and also
whether virtual functions *should* be instantiated, and then    14.7.1/10,


    If a virtual function is implicitly instantiated, its point of
    instantiation is immediately following the point of instantiation
    for its class.


which doesn't make it any more clear.

This might be a defect, since the next hit for the word "virtual" is in
chapter 15, about exceptions...  But I'm not a language lawyer.

I therefore suggest posting this question in [comp.std.c++].



>Reason is that main() I do stuff like
>
>int main() {
>   B1 * b1 = new D1();
>   b1->f(); // should call D<B1>::f()
>   delete b1;
>}


Cheers,

- Alf

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Thu, 10 Oct 2002 19:52:44 +0000 (UTC)
Raw View
Alf P. Steinbach wrote:

> *which leaves it open what "does not require specialization" means*

It's not open, it's covered by 14.7.1/1.

> *and then =A714.7.1/10*

That paragraph does not say what you claim it does.
Do you have an actual copy of the standard? Instead,
14.6.4.1/4 says

     If a virtual function is implicitly instantiated,
     its point of instantiation is immediately following
     the point of instantiation of its enclosing class
     template specialization.

When we finally implement virtual member template methods,
we're going to need runtime template instantiation and
compilation, and the standard had the foresight to allow
implementations to use the same method to delay virtual
method instantiation :-)

---
[ 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://www.jamesd.demon.co.uk/csc/faq.html                       ]