2

In C++ draft standard N3337 section 5.2.10 Reinterpret cast clause 7 (emphasis mine):

An object pointer can be explicitly converted to an object pointer to a different type. When a prvalue v of type “pointer to T1” is converted to the type “pointer to cv T2”, the result is static_cast<cv T2*>(static_cast<cv void*>(v)) if both T1 and T2 are standard-layout types (3.9) and the alignment requirements of T2 are no stricter than those of T1, or if either type is void.

Does this mean that the expression v is a prvalue?. If so, then

int a = 0x1234;
int *pa = &a;
char *pc = reinterpret_cast<char *>(pa);

Above,the variable pa is an lvalue, so can we think if a variable of lvalue is in the right expression, the variable is a prvalue of type?.

xskxzr
  • 12,442
  • 12
  • 37
  • 77
dcthxd
  • 41
  • 4

2 Answers2

2

cppreference is your friend here, if we go to the topic Value categories it tell us a prvalue is:

a prvalue (“pure” rvalue) is an expression whose evaluation either

  • computes the value of the operand of an operator (such prvalue has no result object), or
  • initializes an object or a bit-field (such prvalue is said to have a result object). All class and array prvalues have a result object even if it is discarded. In certain contexts, temporary materialization occurs to create a temporary as the result object;

Which is not automatically enlightening but they provide a very nice set of examples below I will quote some of the pointer examples:

The following expressions are prvalue expressions:

...

  • &a, the built-in address-of expression;
    ...
  • the this pointer;
    ....

So casting a this pointer or the result of address-of to a const of the pointer of that type would fit the wording (assuming that it was not already const) e.g.:

{
  int x = 0;
  const int *p = reinterpret_cast<const int*>(&x);
}
Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
1

The specification uses v to represent an expression. It is not necessarily a name of a variable.

In your example, the paragraph you referred to does not apply directly because pa is an lvalue. Instead, the lvalue-to-rvalue conversion is applied firstly, then this paragraph applies.

xskxzr
  • 12,442
  • 12
  • 37
  • 77
  • Thank you, my understanding for the [5.2.10 `Reinterpret cast` clause 7](https://timsong-cpp.github.io/cppwp/n3337/expr.reinterpret.cast#7) is listed below. First, the `reinterpret_cast(v)` expects the `v` is a prvalue ,that is a subexpression appeared in the right of expression is expected as a prvalue. Second, [5 Expressions clause 8](https://timsong-cpp.github.io/cppwp/n3337/expr#8) and [4.1 Lvalue-to-rvalue conversion clause 1](https://timsong-cpp.github.io/cppwp/n3337/conv.lval#1)explain the conversion is correct. Do I understand correctly above? – dcthxd Oct 29 '18 at 09:58