Consider the following type:
struct S
{
char v;
};
Given an array of const S
, is it possible to, in a standard conformant way, reinterpret it as an array of const char
whose elements correspond to the value of the member v
for each of the original array's elements, and vice-versa? For example:
const S a1[] = { {'a'}, {'4'}, {'2'}, {'\0'} };
const char* a2 = reinterpret_cast< const char* >(a1);
for (int i = 0; i < 4; ++i)
std::cout << std::boolalpha << (a1[i].v == a2[i]) << ' ';
Is the code above portable and would it print true true true true
? If not, is there any other way of achieving this?
Obviously, it is possible to create a new array and initialize it with the member v
of each element of the original array, but the whole idea is to avoid creating a new array.
Aucun commentaire:
Enregistrer un commentaire