Cpp

tuple memory layout

Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <map>
#include <functional>
#include <vector>
#include <string>
#include <iostream>

template<typename... Args>
void func(std::tuple<Args...> const &pair) {
auto &a = std::get<0>(pair);
auto &b = std::get<1>(pair);
auto &c = std::get<2>(pair);
auto &d = std::get<3>(pair);
std::cout << a << ", " << b << ", " << c << ", " << d << "\n";
}

int main () {
std::tuple<int, unsigned short, long, long> pair1{128, 256, 655, 65536};
func(pair1);
std::cout << "Program end\n";
return 0;
}

Disassembly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
main:
subq $40, %rsp
movq $65536, (%rsp)
movq $655, 8(%rsp)
movw $256, 16(%rsp)
movl $128, 20(%rsp)
movq %rsp, %rdi
call void func<int, unsigned short, long, long>(std::tuple<int, unsigned short, long, long> const&)
movl $.LC2, %esi
movl $_ZSt4cout, %edi
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
movl $0, %eax
addq $40, %rsp
ret
# 元素的排列顺序与声明顺序相反
# tuple 的 variadic template deduction 造成的(类的继承关系)
Share