32

Eg with tuples:

#include <tuple>        // std::tuple, std::make_tuple, std::tie

int num;
char letter;
std::tuple<int,char> num_letter;

num_letter = std::make_tuple(10, 'a');
std::tie(num, letter) = num_letter;  // unpack num_letter into num and letter

Is there something equivalent with pairs?

// ...
num_letter = std::make_pair(10, 'a');
std::pair_tie(num, letter) = num_letter;
wrhall
  • 1,288
  • 1
  • 12
  • 26

1 Answers1

56

Actually, the code for pairs is exactly the same, since std::tuple has operator = with std::pair as an argument.

num_letter = std::make_pair(10, 'a');
std::tie(num, letter) = num_letter;
lisyarus
  • 15,025
  • 3
  • 43
  • 68
  • 6
    @wrhall this proves you should at least try the most obvious one :) – bolov Jul 07 '15 at 15:49
  • I was looking at someone else's code, and they were iterating through the elements of a map; I was hopeful that someone would show me something like tie but slightly different that would have made sense to use in that case. I'm not convinced tie is really a readability improvement here, and they were grabbing const references. So it doesn't quite work. The question was more exploratory than anything. – wrhall Jul 07 '15 at 16:40
  • 1
    Nice. Unfortunatelly it doesn't work the other way around `num_letter = std::tie(num, letter)` (no conversion from `tuple<...>` to `pair<...>`). – alfC May 29 '20 at 06:14