[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: questions about c++ templates
From:       Craig Howard <kde () choward ! ca>
Date:       2006-08-18 19:13:25
Message-ID: 200608181213.25941.kde () choward ! ca
[Download RAW message or body]

On Friday 18 August 2006 11:03, Frans Englich wrote:
> However, I think the picture changes a bit when a template contains virtual
> functions. For example, that a template class inherits from a class that
> has pure virtual functions which the template, when instantiated,
> implements.
> 
> In that case I believe it must expand all functions. But of course, dead
> code elimination optimizations can potentially see that it's not needed, as
> for any other case.
> 
> One reason I like templates much is exactly that only the code one needs is
> actually used. One can write a complex class that accounts for much and is
> convenient, but only what is actually needed is expanded.

Interesting.  You are correct:

#include <iostream>

using namespace std;

class Base
{
    public:
        virtual ~Base() {}

        virtual void pure() = 0;
};

template<typename T>
class Child : public Base
{
    public:
        virtual ~Child() {}

        virtual void pure() {}
        void g() { cout << "T1" << endl; }
};

int main()
{
    Child<int> c;

    c.g();

    return 0;
}


12:11:38 [21]; nm -C ./a.out | grep Child                                             \
 080489ba W Child<int>::g()
080488de W Child<int>::pure()
080488c0 W Child<int>::Child()
0804890a W Child<int>::~Child()
08048988 W Child<int>::~Child()
08048b14 V typeinfo for Child<int>
08048b20 V typeinfo name for Child<int>
08048b00 V vtable for Child<int>

Does anybody know why that is?  And why there are two destructors for Child?
-- 

Craig Howard
craig@choward.ca
Software Engineer - Amazon.com
BMath CompSci - University of Waterloo
 
> > Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic