Le problème avec les templates, c'est la gestion de la duplication de code|données:
$ cat *.*pp
#include "File1.hpp"
#include "Template1.hpp"
void File1::func1(){
Template1<int> titi1;
titi1.add(1, 2);
}
#ifndef FILE1_HPP
#define FILE1_HPP
class File1{
public:
void func1();
};
#endif
#include "File2.hpp"
#include "Template1.hpp"
void File2::func2(){
Template1<int> titi2;
titi2.add(1, 2);
}
#ifndef FILE2_HPP
#define FILE2_HPP
class File2{
public:
void func2();
};
#endif
#ifndef TEMPLATE1_HPP
#define TEMPLATE1_HPP
template <typename T>
class Template1{
public:
T add(T a, T b){
volatile int keep;
keep = *((int*)"HER\n");
return a + b;
}
};
#endif
#include "File1.hpp"
#include "File2.hpp"
int main(int nArg, char **pArg){
File1 f1;
File2 f2;
f1.func1();
f2.func2();
return 0;
}
$ gcc -S -fno-default-inline *.cpp
$ cat *.s
.file "File1.cpp"
.text
.align 2
.globl __ZN5File15func1Ev
.def __ZN5File15func1Ev; .scl 2; .type 32; .endef
__ZN5File15func1Ev:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $2, 8(%esp)
movl $1, 4(%esp)
leal -1(%ebp), %eax
movl %eax, (%esp)
call __ZN9Template1IiE3addEii
leave
ret
LC0:
.ascii "HER\12\0"
.section .text$_ZN9Template1IiE3addEii,"x"
.linkonce discard
.align 2
.globl __ZN9Template1IiE3addEii
.def __ZN9Template1IiE3addEii; .scl 2; .type 32; .endef
__ZN9Template1IiE3addEii:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl LC0, %eax
movl %eax, -4(%ebp)
movl 16(%ebp), %eax
addl 12(%ebp), %eax
leave
ret
.def __ZN9Template1IiE3addEii; .scl 3; .type 32; .endef
.file "File2.cpp"
.text
.align 2
.globl __ZN5File25func2Ev
.def __ZN5File25func2Ev; .scl 2; .type 32; .endef
__ZN5File25func2Ev:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $2, 8(%esp)
movl $1, 4(%esp)
leal -1(%ebp), %eax
movl %eax, (%esp)
call __ZN9Template1IiE3addEii
leave
ret
LC0:
.ascii "HER\12\0"
.section .text$_ZN9Template1IiE3addEii,"x"
.linkonce discard
.align 2
.globl __ZN9Template1IiE3addEii
.def __ZN9Template1IiE3addEii; .scl 2; .type 32; .endef
__ZN9Template1IiE3addEii:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl LC0, %eax
movl %eax, -4(%ebp)
movl 16(%ebp), %eax
addl 12(%ebp), %eax
leave
ret
.def __ZN9Template1IiE3addEii; .scl 3; .type 32; .endef
.file "main.cpp"
.def ___main; .scl 2; .type 32; .endef
.text
.align 2
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
movl %eax, -8(%ebp)
movl -8(%ebp), %eax
call __alloca
call ___main
leal -1(%ebp), %eax
movl %eax, (%esp)
call __ZN5File15func1Ev
leal -2(%ebp), %eax
movl %eax, (%esp)
call __ZN5File25func2Ev
movl $0, %eax
leave
ret
.def __ZN5File25func2Ev; .scl 3; .type 32; .endef
.def __ZN5File15func1Ev; .scl 3; .type 32; .endef
.file "File1.cpp"
.text
.align 2
.globl __ZN5File15func1Ev
.def __ZN5File15func1Ev; .scl 2; .type 32; .endef
__ZN5File15func1Ev:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $2, 8(%esp)
movl $1, 4(%esp)
leal -1(%ebp), %eax
movl %eax, (%esp)
call __ZN9Template1IiE3addEii
leave
ret
LC0:
.ascii "HER\12\0"
.section .text$_ZN9Template1IiE3addEii,"x"
.linkonce discard
.align 2
.globl __ZN9Template1IiE3addEii
.def __ZN9Template1IiE3addEii; .scl 2; .type 32; .endef
__ZN9Template1IiE3addEii:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl LC0, %eax
movl %eax, -4(%ebp)
movl 16(%ebp), %eax
addl 12(%ebp), %eax
leave
ret
.def __ZN9Template1IiE3addEii; .scl 3; .type 32; .endef
.file "File2.cpp"
.text
.align 2
.globl __ZN5File25func2Ev
.def __ZN5File25func2Ev; .scl 2; .type 32; .endef
__ZN5File25func2Ev:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
movl $2, 8(%esp)
movl $1, 4(%esp)
leal -1(%ebp), %eax
movl %eax, (%esp)
call __ZN9Template1IiE3addEii
leave
ret
LC0:
.ascii "HER\12\0"
.section .text$_ZN9Template1IiE3addEii,"x"
.linkonce discard
.align 2
.globl __ZN9Template1IiE3addEii
.def __ZN9Template1IiE3addEii; .scl 2; .type 32; .endef
__ZN9Template1IiE3addEii:
pushl %ebp
movl %esp, %ebp
subl $4, %esp
movl LC0, %eax
movl %eax, -4(%ebp)
movl 16(%ebp), %eax
addl 12(%ebp), %eax
leave
ret
.def __ZN9Template1IiE3addEii; .scl 3; .type 32; .endef
.file "main.cpp"
.def ___main; .scl 2; .type 32; .endef
.text
.align 2
.globl _main
.def _main; .scl 2; .type 32; .endef
_main:
pushl %ebp
movl %esp, %ebp
subl $24, %esp
andl $-16, %esp
movl $0, %eax
movl %eax, -8(%ebp)
movl -8(%ebp), %eax
call __alloca
call ___main
leal -1(%ebp), %eax
movl %eax, (%esp)
call __ZN5File15func1Ev
leal -2(%ebp), %eax
movl %eax, (%esp)
call __ZN5File25func2Ev
movl $0, %eax
leave
ret
.def __ZN5File25func2Ev; .scl 3; .type 32; .endef
.def __ZN5File15func1Ev; .scl 3; .type 32; .endef
$ gcc -fno-default-inline *.cpp
$ grep -uc HER a.exe
2