I have newtypes around int, as well as classes that contains those newtype.
class IndexA {
int i;
};
class A {
IndexA index;
};
// this mimic exactly the hierarchy of `A`
struct IndexB {
int i;
};
struct B {
IndexB index;
};
Given that A and B share the exact same binary layout, does the following code contains undefined behavior or is it safe?
std::vector<A> vfoo {...};
std::vector<B> vbar {
std::move(
reinterpret_cast<std::vector<B>&>(vfoo)
)
};
I would have liked to be able to do a zero copy move transformation from std::vector<A> to std::vector<B>. Is it possible?
In my code:
AandBare really just a wrapper aroundint, so they have exactly the same binary representation.- The struct
Aonly contains a mix ofIndexA,std::optional<IndexA>,std::variant<IndexA, other types like IndexA>, orstd::vector<IndexA or std::optional<IndexA>, …>. I think that I can assume that they have the same representation. Bis a strict copy ofA, where every instances ofIndexAare replaced byIndexB. The fields order is kept.