Title
Array-to-pointer conversion with object type mismatch
Status
open
Section
7.3.3 [conv.array]
Submitter
Andrey Erokhin

Created on 2020-10-28.00:00:00 last changed 60 months ago

Messages

Date: 2020-10-28.00:00:00

(From submission #707.)

Consider:

  int a[5];
  int *p1 = &a[0];        // points to the first element of a
  auto pa = reinterpret_cast<int(*)[5]>(p1);
  int *p2 = *pa;              // valid array-to-pointer conversion?

If a glvalue of array type is subject to the array-to-pointer conversion, the behavior should be undefined if the type of the result of the glvalue is not an array (see 7.6.1.5 [expr.ref] paragraph 10 for the class member access case).

Possible resolution:

Change in 7.3.3 [conv.array] as follows:

An lvalue or rvalue expression E of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The If E is a prvalue, the temporary materialization conversion (7.3.5 [conv.rval]) is applied. The If the result of the (possibly materialized) E is an object whose type is similar to the type of E, the result is a pointer to the first element of the array, otherwise the behavior is undefined.
History
Date User Action Args
2020-10-28 00:00:00admincreate