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 ;


 
User Using
  • 633
  • 1
  • 5
  • 15
Rahil
  • 23
  • 5

2 Answers2

3

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,021
  • 7
  • 13
0

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]]

User Using
  • 633
  • 1
  • 5
  • 15