1

What I know is:

  1. An L-Value is an expression (could be a single variable expression i.e x) which has some address (meaning it's stored in memory and can be referred to, beyond the scope of current expression).

  2. "this pointer" points to the object itself, which means it holds the address of the object which in turn means it's an L-Value (by definition in point 1).
What I want to know is:

  1. Is "this pointer" really an L-Value ? Because in a post " Type of 'this' pointer " people are saying that "this pointer" is not an L-Value

  2. If "this pointer" is not an L-Value then is it an R-Value or something else ?
Community
  • 1
  • 1
Danish ALI
  • 65
  • 1
  • 9
  • The post you linked to seems to explain the issue quite well. – Jonathan Potter Nov 23 '14 at 23:09
  • 1
    That post is little different and is in different context I just need to the point answer that's why I post my question in bullet form. I mean the yes/no type of answers. – Danish ALI Nov 23 '14 at 23:13
  • BUT vsoftco in the post which I have referred to in my question many people are confidently saying that "this pointer" is not an L-Value or they are saying "this pointer" is a R-Value , that's why I'm confused. because I think "this pointer" is an L-Value and people in that post are saying it's not an L-Value – Danish ALI Nov 23 '14 at 23:22
  • 1
    *"An L-Value is an expression [...] which has some address"* Expressions don't have addresses. Regions of memory (i.e., objects) have addresses. An expression can refer to an object, or yield an object (amongst other things). – dyp Nov 24 '14 at 00:29
  • Understanding value categories requires understanding language abstractions, something many programmers struggle with and can only really be gained through experience and study. – Lightness Races in Orbit Nov 24 '14 at 00:57

1 Answers1

6

Your basic description of lvalues is a fairly good general rule from a naive perspective, but it falls apart in the presence of lvalue-to-rvalue conversions for expression evaluation, and it doesn't account for notable exceptions such as the this keyword.

Since this itself is not a pointer that you can modify or deem to really be "stored" anywhere — recall that its value is automatically determined by the compiler based on context — the committee decided that it would be best off as an rvalue expression.

Accordingly, your reasoning about memory addresses is not quite right: this, as a pointer, certainly contains a memory address as its physical value (on typical systems) but that doesn't mean it has one for itself. Try taking its address: &this can't work.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I like this explanation (and it directly answers the OP's question). Provide the reference to the committee decision and I'll upvote this ;-) – jdigital Nov 23 '14 at 23:27
  • Then what is guess is, This is like a constant variable whose address is not visible to programmer and also the programmer can't modify it but yet it's address is known internally to the compiler and it's value is changed by the compiler itself based on the context, right? – Danish ALI Nov 23 '14 at 23:38
  • Not quite: the address of a const variable is visible to a programmer. – jdigital Nov 23 '14 at 23:43
  • I'm sorry I should have phrased like "This is like a constant variable but it's address is not visible........." I meant "like constant" in the context of unmodifiable not visibility. and in the case of "this pointer" neither we can know the address of the pointer itself nor we can take it's value (address of object to whom "this" points to, I mean can't do like this myptr = this;) right? – Danish ALI Nov 23 '14 at 23:51
  • @jdigital: The standard is the reference to the committee decision. That is, if the committee hadn't decided to make `this` an _rvalue_ expression, then it would not be one! – Lightness Races in Orbit Nov 24 '14 at 00:54
  • @DanishALI: You can take its value of course, otherwise you'd never be able to actually use it. Otherwise correct. – Lightness Races in Orbit Nov 24 '14 at 00:55
  • Oh! I get it, means "myptr = this" is alright but "myrfr = &this" is illegal. – Danish ALI Nov 24 '14 at 13:34