Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does pair.first return a reference to the first value?

In the C++ standard library, there is an object called the pair. Pair.first and Pair.second return the first and second values of the pair object, respectively. I want to increment the first value by one under a conditional (my pair is a pair). It's throwing "needs 1-value" when I try to do this. This could be because somewhere along the way I am not passing in a value by reference, but it could also be that .first does not return the first value of the pair by reference. Does anyone know if it does?

like image 823
keyert Avatar asked Jun 17 '26 19:06

keyert


1 Answers

first is not a function, it is an object itself, a data member of struct pair. The name of an object is an lvalue, equivalent to a reference.

"Lvalue" is a language term denoting something that can go on the left-hand side of an assignment, i.e. a value can be assigned to it, or it has a meaningful address in memory.

The result of a conditional expression is an lvalue if both its alternatives are lvalues of the same type.

For example,

pair< int, int > p;
flag? p.first : p.second = 34; // ok

flag? p.first : 3 = 15; // error: 3 is not an lvalue; 3 = 15 is nonsense

pair< int, short > q;
flag? q.first : q.second = 12; // error: different types

Note, the opposite of "lvalue" is "rvalue," for right-hand side of an assignment. These terms are borrowed from the C language.

Remember: the lvalue of an expression is where it is stored, the rvalue is its content. Which of these an expression meaningfully has decide its value category.

like image 123
Potatoswatter Avatar answered Jun 19 '26 09:06

Potatoswatter