0

I have a tuple with some elements, and I want to assign some elements of tuple to variables, and ignore some of them.

 auto tuple1 = std::make_tuple(1,2,3,4);
 // variable a should be placeholder 1 in tuple and variable b should be place holder 3;
 int a,b ;


 
UserUsing
  • 678
  • 1
  • 5
  • 16
Rahil
  • 23
  • 5

2 Answers2

4

You could use from std::tie and std::ignore in tuple such as :

 int a, b;
 tie(std::ignore, a, std::ignore, b)= tuple1;
Farhad Sarvari
  • 1,051
  • 7
  • 13
1

If you use the strutural binding along with C++20 support, you could write

#include <tuple>    

auto tuple1 = std::make_tuple(1, 2, 3, 4);
[[maybe_unused]] auto [dummy1, a, dummy2, b] = tuple1;

Use only a and b. The [[maybe_unused]] part is to suppres the warning due to -Wunused-variable.

This is actually inspired from the answer: structured binding with [[maybe_unused]]

UserUsing
  • 678
  • 1
  • 5
  • 16