简体   繁体   中英

What does this mean "prvalue of type“ in C++ standard N3337 5.2.10 clause 7?

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?.

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) eg:

{
  int x = 0;
  const int *p = reinterpret_cast<const int*>(&x);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM