Aliasing with an array

94 views
Skip to first unread message

xsk...@gmail.com

unread,
Dec 31, 2017, 10:41:28 PM12/31/17
to ISO C++ Standard - Discussion
Hi all,

Please consider the following example:
int a[3] = {0};
auto &b = reinterpret_cast<int(&)[2]>(a);
int c = b[0];

It seems that the value of a has not been accessed until b[0], which is equivalent to *(b + 0), thus access an int object through an int glvalue. 

So Is this well-defined?

Thanks.
xskxzr

Nicol Bolas

unread,
Jan 1, 2018, 12:01:59 AM1/1/18
to ISO C++ Standard - Discussion
What about this code would not be well-defined?

xskxzr

unread,
Jan 1, 2018, 1:31:29 AM1/1/18
to ISO C++ Standard - Discussion
It is a bit strange. If it is well-defined, what is the semantic of the array-to-pointer conversion while the array object actually does not exist?

T. C.

unread,
Jan 2, 2018, 12:40:10 PM1/2/18
to ISO C++ Standard - Discussion
If there is no such array object, then the behavior is undefined by omission.

xskxzr

unread,
Jan 2, 2018, 10:39:50 PM1/2/18
to ISO C++ Standard - Discussion
So do you mean the example is undefined?

Chris Hallock

unread,
Jan 3, 2018, 2:10:09 AM1/3/18
to ISO C++ Standard - Discussion

On Sunday, December 31, 2017 at 10:31:29 PM UTC-8, xskxzr wrote:
It is a bit strange. If it is well-defined, what is the semantic of the array-to-pointer conversion while the array object actually does not exist?

Well, but the expression does refer to an actual array, just not one that has the expression's type. The good question I think you're asking is, when [conv.array] says "the result is a pointer to the first element of the array", what exactly is meant by "the array"?
  1. the (presumed-to-exist) array referred to by the expression, or
  2. the (presumed-to-exist) array of the expression's type referred to by the expression?
I think the correct interpretation is #1, because #2 has the absurd implication of not allowing differences in cv-qualification (unless that in itself is a defect...).

xskxzr

unread,
Jan 3, 2018, 7:27:10 AM1/3/18
to ISO C++ Standard - Discussion
Then what if the expression refers to an non-array object? For example, change a from int[3] to int. Or do you mean the object is considered an array with a single element in such case?

Chris Hallock

unread,
Jan 3, 2018, 3:53:34 PM1/3/18
to ISO C++ Standard - Discussion


On Wednesday, January 3, 2018 at 4:27:10 AM UTC-8, xskxzr wrote:
Then what if the expression refers to an non-array object? For example, change a from int[3] to int. Or do you mean the object is considered an array with a single element in such case?

Undefined-by-omission, as T.C. says. The "1-element array" clause doesn't help because it only applies to pointer arithmetic/comparison with the result of operator &.
Reply all
Reply to author
Forward
0 new messages