GDB: How do we extract values ​​from std :: tuple

How do we extract / print single values ​​in std :: tuple?

Here is an example program in the test.cc file.

#include <tuple>
#include <iostream>

 using namespace std;

 int main() {
     auto t = make_tuple(111, 222);
     cout << std::get<0>(t) << endl
          << std::get<1>(t) << endl;
     return 0;
 }

Compile it

g++ --std=c++11 -g test.cc

Run it in gdb

gdb --args ./a.out
...
(gdb) start
Temporary breakpoint 1 at 0x400836: file test.cc, line 7.
Starting program: /home/fmlheureux/a.out

Temporary breakpoint 1, main () at test.cc:7
7           auto t = make_tuple(111, 222);
(gdb) n
9                << std::get<1>(t) << endl;
(gdb) p t
$1 = std::tuple containing = {[1] = 111, [2] = 222}

The last command printed the tuple as a whole. How can I extract individual values? My naive attempts fail.

(gdb) p get<0>(t)
No symbol "get<0>" in current context.
(gdb) p std::get<0>(t)
No symbol "get<0>" in namespace "std".
+4
source share
1 answer

Unfortunately, the pretty-printed code in gdb is a display-only function, so when it helps to display a tuple in a great way, it does not allow you to access it.

- t.get<0> , , . , gdb "xmethod", Python gdb, info xmethods ( ), std::tuple.

, : . , :

(gdb) p/r t
$3 = {<std::_Tuple_impl<0ul, int, int>> = {<std::_Tuple_impl<1ul, int>> = {<std::_Head_base<1ul, int, false>> = {
        _M_head_impl = 222}, <No data fields>}, <std::_Head_base<0ul, int, false>> = {_M_head_impl = 111}, <No data fields>}, <No data fields>}

"" :

(gdb) print ((std::_Head_base<1ul, int, false>) t)._M_head_impl
$7 = 222

- , ? , gdb _M_head_impl. , xmethod. Python ; API Python.

+4

Source: https://habr.com/ru/post/1648791/


All Articles