delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/16/11:31:22

From: "Alexander S. Klenin" <root AT kern DOT marine DOT su>
Newsgroups: comp.os.msdos.djgpp
Subject: Template inlining
Date: Thu, 16 Oct 97 19:10:28 +1100
Distribution: world
Organization: TOO Kern
Message-ID: <AAqlSHq4I4@kern.marine.su>
Sender: news-service AT kiae DOT su
Reply-To: root AT kern DOT marine DOT su
Lines: 98
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hi,
Here is a (long) question:

I have a following class template:

// test.h
template <class T>
class C
{ T a; // some data here...
public:
  C(const T &t): a(t) {} // some useful functions...
};

And then use it:

//test.cpp
#include"test.h"
main()
{ C<int> c(5);
}

Template functions are not inlined, so I got:

        .file   "test.cc"
gcc2_compiled.:
___gnu_compiled_cplusplus:
.text
.globl _main
_main:
        pushl %ebp
        movl %esp,%ebp
        subl $8,%esp
        movl $5,-8(%ebp)
        leal -8(%ebp),%edx
        leal -4(%ebp),%eax
        call ___t1C1ZiRCi     <----- This call is not inlined :(
        xorl %eax,%eax
        leave
        ret
___t1C1ZiRCi:
        pushl %ebp
[code skipped]
        leave
        ret

To fix this, I pre-declared template instantiation:

//test1.cpp
template class C<int>;
main()
{ C<int> c(5);
}

And then functions get inlined indeed, but they are
also generated as non-inline, this time _all_ of them, not
only those I call:

        .file   "test.cc"
gcc2_compiled.:
___gnu_compiled_cplusplus:
.text
.globl ___t1C1ZiRCi
___t1C1ZiRCi:
        pushl %ebp
[some code]
        leave
        ret
.globl _main
_main:
        pushl %ebp
        movl %esp,%ebp
        subl $8,%esp
        movl $5,-8(%ebp)   <----- now inline works ok
        movl $5,-4(%ebp)
        xorl %eax,%eax
        leave
        ret
.globl ___as__t1C1ZiRCt1C1Zi
___as__t1C1ZiRCt1C1Zi:
        pushl %ebp
[some unneeded code]
        leave
        ret
.globl ___t1C1ZiRCt1C1Zi
___t1C1ZiRCt1C1Zi:
        pushl %ebp
[even more code]
        leave
        ret

Even worse, since all the above declared with .globl,
all the bloat gets linked right into .exe!

No the question is: is there any way to avoid this?

Alex


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019