I have a very simple code as below, which use C vector:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> g1;
return 0;
}
By coping the code to the website https://godbolt.org/, I know the generated assembly code is as below:
main:
stp x29, x30, [sp, -64]!
mov x29, sp
str x19, [sp, 16]
add x0, sp, 40
bl std::vector<int, std::allocator<int> >::vector()** [complete object constructor]
mov w19, 0
add x0, sp, 40
bl std::vector<int, std::allocator<int> >::~vector() [complete object destructor]
mov w0, w19
ldr x19, [sp, 16]
ldp x29, x30, [sp], 64
ret
__static_initialization_and_destruction_0(int, int):
stp x29, x30, [sp, -32]!
mov x29, sp
str w0, [sp, 28]
str w1, [sp, 24]
ldr w0, [sp, 28]
cmp w0, 1
bne .L27
ldr w1, [sp, 24]
mov w0, 65535
cmp w1, w0
bne .L27
adrp x0, _ZStL8__ioinit
add x0, x0, :lo12:_ZStL8__ioinit
bl std::ios_base::Init::Init() [complete object constructor]
adrp x0, __dso_handle
add x2, x0, :lo12:__dso_handle
adrp x0, _ZStL8__ioinit
add x1, x0, :lo12:_ZStL8__ioinit
adrp x0, _ZNSt8ios_base4InitD1Ev
add x0, x0, :lo12:_ZNSt8ios_base4InitD1Ev
bl __cxa_atexit
.L27:
nop
ldp x29, x30, [sp], 32
ret
_GLOBAL__sub_I_main:
stp x29, x30, [sp, -16]!
mov x29, sp
mov w1, 65535
mov w0, 1
bl __static_initialization_and_destruction_0(int, int)
ldp x29, x30, [sp], 16
ret
DW.ref.__gxx_personality_v0:
.xword __gxx_personality_v0
Now my question is where I can find the code of std::vector<int, std::allocator >::vector().
If I define a new template class in my code as below:
#include <iostream>
using namespace std;
template <class T, class U> class A {
T x;
U y;
public:
A() { cout << "Constructor Called" << endl; }
};
int main()
{
A<char, char> a;
A<int, double> b;
return 0;
}
I know the code of A<char, char> and A<int, double> is generated in the object file of my own code. But for the template class and template function of C STL, where is the code of their instantiated object?
There is in the library file of C standard library, e.g. libstdc .so, but I think compiler will not generate the code inside that .so file.
Thanks!
CodePudding user response:
I know the code of A<char, char> and A<int, double> is generated in the object file of my own code. But for the template class and template function of C STL, where is the code of their instantiated object?
Exactly the same: they are templated types, and only your specific instantiation makes std::vector<int, std::allocator >::vector()
exist.
You'll need to think about this in terms of translation units; so, this machine code will be found in each translation unit that includes the header and instantiates that specific type.