Is it not possible to call untie?

int rno; string name; int marks;
tuple <int, string, int> x=make_tuple(1, "anukul", 100);
tie(rno, name, marks)=x;

This piece of code assigns values ​​in a tuple x to variables. In some way, it unpacks the tuple x.

So why is the function called 'tie'? What does he bind?

cplusplus states that it "associates arguments with tuple elements." But changing tuple elements in variables is not reflected.

+4
source share
3 answers

You should read the documentation for std :: tie

Creates a tuple of lvalue references to its arguments [...]

therefore, in addition to using a tuple to “unpack” a tuple into variables, just like you, you can also use it as follows

int rno = 10; string name; int marks;
tuple <int&, string&, int&> x = tie(rno, name, marks);
get<0>(x) = 42;
cout << rno; // 42

"" -, lvalue.

utnapistim ( ) , "", - -

struct comparable { 
  int a, b; 
  bool operator==(const comparable& x) { 
    return std::tie(a, b) == std::tie(x.a, x,b); 
  }
};
+10

rho, name marks , ( ). , ( ), , ( ).

, std::tie — . , x . , , .

+7

No.

If you have a reasonable argument that you are disconnecting x, you are not working with xwhere you wrote std::tie.

Instead, you work with rno, nameand marks, which are related to the assignment.

+3
source

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


All Articles