2011年12月27日 星期二

[C++] template class instantiation

驗證一下, 在template class當中, 如果有method不依靠template class type T, 這樣還是會產生出兩份不一樣的code, 因為template本身在instantiation的時候就會被當作兩個不同的class.


#include <iostream>
template
class Foo
{
public:
T goo(T a) { return a + a; };
int foo(int a) { return a;};
};

int main()
{
Foo a;
Foo b;

a.goo(12);
b.goo(1.2);
a.foo(1);
b.foo(1);
return 0;
}

可以看到main裡面, _ZN3FooIiE3fooEi, _ZN3FooIdE3fooEi 是被call到兩個不同的function, 就算裡面code是完全一樣的


.file "template_gen.cpp"
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.text
.globl main
.type main, @function
main:
.LFB959:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
subq $16, %rsp
leaq -1(%rbp), %rax
movl $12, %esi
movq %rax, %rdi
call _ZN3FooIiE3gooEi
movsd .LC0(%rip), %xmm0
leaq -2(%rbp), %rax
movq %rax, %rdi
call _ZN3FooIdE3gooEd
leaq -1(%rbp), %rax
movl $1, %esi
movq %rax, %rdi
call _ZN3FooIiE3fooEi
leaq -2(%rbp), %rax
movl $1, %esi
movq %rax, %rdi
call _ZN3FooIdE3fooEi
movl $0, %eax
leave
ret
.cfi_endproc

...略

_ZN3FooIiE3fooEi:
.LFB962:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
movq %rdi, -8(%rbp)
movl %esi, -12(%rbp)
movl -12(%rbp), %eax
leave
ret
.cfi_endproc
.LFE962:
.size _ZN3FooIiE3fooEi, .-_ZN3FooIiE3fooEi
.section .text._ZN3FooIdE3fooEi,"axG",@progbits,_ZN3FooIdE3fooEi,comdat
.align 2
.weak _ZN3FooIdE3fooEi
.type _ZN3FooIdE3fooEi, @function
_ZN3FooIdE3fooEi:
.LFB963:
.cfi_startproc
.cfi_personality 0x3,__gxx_personality_v0
pushq %rbp
.cfi_def_cfa_offset 16
movq %rsp, %rbp
.cfi_offset 6, -16
.cfi_def_cfa_register 6
movq %rdi, -8(%rbp)
movl %esi, -12(%rbp)
movl -12(%rbp), %eax
leave
ret
.cfi_endproc

...略