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

List:       kde-devel
Subject:    Re: questions about c++ templates
From:       Benoît_Jacob <jacob () math ! jussieu ! fr>
Date:       2006-08-18 17:14:31
Message-ID: 200608181914.31950.jacob () math ! jussieu ! fr
[Download RAW message or body]

thanks very, very much! Very useful explanation.

Benoit

> > If it doesn't share code, do you have a clue why ? At first sight, one
> > would think that from a low-level point of view, all pointer types are
> > equivalent (to, say, void*) and thus there shouldn't be any difference
> > between M<some_type *> and M<some_other_type *>, so code could be
> > shared... ?
>
> Grrr, hit the send button too early.
>
> It can't share code, because you could have the following:
>
> template<typename T>
> void f(T t)
> {
> 	t->g();
> }
>
> Where some_type may have g(), but some_other_type may not.  Or, in a more
> complicated situtation, they may both have g(), and both g()s are declared
> virtual, but each g() lives in different spots in the vtable, so the same
> calling code would end up calling different methods if the offset into the
> vtable was the same because they were running the same generated code.
>
> Now if you can guarantee none of the above situations are going to happen
> (ie two classes with identical definitions), then I suppose you _could_
> merge the two.  I'm not sure what the standard says.  However, that's a
> fair bit of work for the compiler for what's likely a rare situation.  g++
> does not do it.
>
> Here's a test program to confirm:
> #include <iostream>
>
> using namespace std;
>
> class T1
> {
>     public:
>         void g() { cout << "T1" << endl; }
> };
>
> class T2
> {
>     public:
>         void g() { cout << "T2" << endl; }
> };
>
>
> template<typename T>
> void f(T t)
> {
>     t->g();
> }
>
> int main()
> {
>     T1 t1;
>     T2 t2;
>
>     f(&t1);
>     f(&t2);
>
>     return 0;
> }
>
> 10:02:38 [9]; nm -C ./a.out| grep 'void f'
> 08048708 W void f<T1*>(T1*)
> 08048748 W void f<T2*>(T2*)
>
> Note that the two f()'s have different addresses.
 
>> 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