vendredi 22 juillet 2016

How do we extract values from an std::tuple

How do we extract/print individual values in an std::tuple?

Here is a sample program in file named test.cc.

#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 naives 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".

Aucun commentaire:

Enregistrer un commentaire