Reinterpret casting to array types

Skip to first unread message

Jun 15, 2014, 11:10:02 AM6/15/14

Does the standard allow/consider to be defined behavior the following?

T arr[100];

using T_part = T[10];

T_part *a = reinterpret_cast<T_part*>(&arr[5]);
(*a)[1] = some_value;
assert(arr[6] == some_value);

In this way T_part would become a slice of arr from 5 to 15.

Does this depend on whether T is char, another POD type, or a non-POD type?

If this isn't permitted, why not? I suppose potentially it could
interfere with alias analysis, but maybe existing compilers already
don't assume that arrays of different sizes can't alias each other.
Clearly it would be very useful.

Similarly, it would be desirable to be able to do this with std::array
or a similar type wrapping an array rather than with a raw array.

As a related matter, if we have a POD or non-POD struct X, we might
want to define:

class Y {
X x;
// some methods here

and then reinterpret_cast from X * to Y *.

[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: ]

Reply all
Reply to author
0 new messages