7

I just read the C reference about prefix increment operator and realized that the result of prefix increment operator are not lvalue, but it's surprising that it is lvalue in C++. After that I read this answer which explaining why it's a lvalue, but I don't understand it:

(Line 3): ["] it appears that it is so you can take its address or assign to a reference. [."]

and an example follows:

int i;
extern void f (int* p);

f (&++i);   /* Would be illegal C, but C programmers
              havent missed this feature */
...

So what's the merit of allowing this? Is the only purpose of this that incrementing i in global region is illegal? If this is the only reason I would consider this be a remedy for a defect in C that cannot/hard to be resolved, or the program should probably be rewritten for the sake of readability, right?

btw I don't understand why lvalue is also called "locator value", I've read this - line 4 but locator is vague for me. What's a locator, is it a pointer something?

EDIT: For the sake of your precious time reading about wth is locator value, here is my homemade backronym:

  • lvalue: location value, you know the location of it.
  • rvalue: read value, you can only read the value.

don't blame me if anything gone wrong.

Kindred
  • 1,229
  • 14
  • 41
  • 4
    Interesting observation. I'd venture a guess that if you delve into operator overloading, then this type of usage might make sense. Which would explain why it's part of C++ but not C. – Andrew Henle Nov 21 '18 at 13:43
  • related/dupe: https://stackoverflow.com/questions/371503/why-is-i-considered-an-l-value-but-i-is-not. I think the top answer has the best reasoning I've seen. IT wasn't possible to do this in C since C doesn't have references but C++ does so it would be a natural extension. – NathanOliver Nov 21 '18 at 13:47
  • 2
    I have never seen "locator value" before, and suspect it's a backronym. As far as I know, l-value and r-value originally referred to the left and right side of an assignment expression. – Tim Randall Nov 21 '18 at 13:49
  • 1
    @TimRandall It's actually in the C99 standard, which surprised me. – NathanOliver Nov 21 '18 at 13:50
  • @NathanOliver is there a corresponding "meaning" for r-value? – Tim Randall Nov 21 '18 at 13:52
  • In C99 *value of an expression* – NathanOliver Nov 21 '18 at 13:54
  • This is definitely a dupe but I can't find the canonical one. – Lundin Nov 21 '18 at 14:39
  • @ptr_user7813604 If not even veteran users can find it, then we clearly can't blame you for not finding it either. The SO duplicate system isn't great. – Lundin Nov 21 '18 at 15:03

1 Answers1

7

In C++, prefix ++ giving lvalue is actually very natural.

Because C++ has operator overloading. For most iterators of potentially complicated type, the prefix ++ returns the lvalue of the iterator itself.

Thus for generic programming, it would be inconvenient to make fundamental type a special case.

For example;

auto &iter = ++old_iter;

wouldn't work if prefix increment of pointer gives an rvalue.

llllllllll
  • 16,169
  • 4
  • 31
  • 54
  • So principle of least privilege saves the day right? Allowing it to be an lvalue on primitive type doesn't cause any trouble. C is the best, I knew it. (not to offend, C++ is also the best :)) – Kindred Nov 21 '18 at 14:04
  • I'm not buying it. As far as I can tell, C++ could have the same rule as C and still implement overloaded `++` operators the same way. It affects how you can *use* the result, not necessarily how you must *compute* the result. – John Bollinger Nov 21 '18 at 14:06