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

List:       kde-devel
Subject:    Re: questions about c++ templates
From:       Frans Englich <englich () kde ! org>
Date:       2006-08-18 17:50:43
Message-ID: 200608181803.48331.englich () kde ! org
[Download RAW message or body]

On Friday 18 August 2006 16:32, Craig Howard wrote:
> On Friday 18 August 2006 09:11, Benoît Jacob wrote:
> > Hi!
> >
> > I've got a few questions, so if you can help me that'll result in a
> > better kdesupport/eigen library ;)
> >
> > 1. when you instantiate a template class, does the compiler generate code
> > only for those methods that are getting called for this particular
> > template instantiation, or does it generate code for all methods?
> >
> > Concretely:
> >
> > -------------------------------------------------------------------
> > template<class T> class M
> > {
> > 	// 500 methods that DON'T call one another
> > 	void method1();
> > 	...
> > 	void method500();
> > };
> >
> > ...
> >
> > M<int> m;
> > m.method1();
> > -------------------------------------------------------------------
> >
> > does that generate code for the 499 unused methods of M<int> ?
>
> No.  (Although it may be compiler dependent; a recent version of g++ does
> it right)

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.


Cheers,

		Frans
 
>> 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