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

List:       kde-devel
Subject:    Re: questions about c++ templates
From:       Krzysztof Lichota <krzysiek () lichota ! net>
Date:       2006-08-19 8:13:20
Message-ID: 44E6C820.9090707 () lichota ! net
[Download RAW message or body]

[Attachment #2 (multipart/signed)]


Craig Howard napisał(a):
> 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)

Well, it depends how instantiation is done.
If you put the code in header file and any user of header can
instantiate the code, then I guess unnecessary methods are not
generated. But if you create a library (which I assume is what will be
done for eigen library) and you do explicit instantiation for some
types, then:

$ cat a.h
template<class T>
class M
{
public:
        // 500 methods that DON'T call one another
        void method1();
        void method2();
        void method500();
};

$ cat a.cpp
#include <iostream>
#include "a.h"

using namespace std;

template <class T>
void M<T>::method1()
{
}

template <class T>
void M<T>::method2()
{
}

template <class T>
void M<T>::method500()
{
}

template class M<int>;

Then:
$ g++ -c a.cpp -o a.o
$ g++ -shared a.o -o a.so
$ nm -C ./a.so  | grep method
000006d6 W M<int>::method1()
000006dc W M<int>::method2()
000006e2 W M<int>::method500()

HTH

	Krzysztof Lichota


["signature.asc" (application/pgp-signature)]

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