D
Daniel Pitts
If I have a recursive template such as:
template <typename T, unsigned size>
struct PlaceHolder {
typedef PlaceHolder<T, size-1> Next;
T value;
Next next;
PlaceHolder(const PlaceHolder &placeHolder)
: value(placeHolder.value), next(placeHolder.next) {}
PlaceHolder(const T& value, const Next &next)
: value(value), next(next) {}
};
template <typename T>
struct PlaceHolder<T, 0> {
T value;
PlaceHolder(const PlaceHolder &placeHolder) : value(placeHolder.value) {}
PlaceHolder(const T& value) : value(value) {}
};
Can I make any assumptions/assertions about the memory layout of any
particular PlaceHolder<X,Y> struct?
In particular, is the following function valid, or is it UB?
template <typename T, unsigned size>
T &get(PlaceHolder<T, size> &place, int index) {
return reinterpret_cast<T*>(&place)[index];
}
template <typename T, unsigned size>
struct PlaceHolder {
typedef PlaceHolder<T, size-1> Next;
T value;
Next next;
PlaceHolder(const PlaceHolder &placeHolder)
: value(placeHolder.value), next(placeHolder.next) {}
PlaceHolder(const T& value, const Next &next)
: value(value), next(next) {}
};
template <typename T>
struct PlaceHolder<T, 0> {
T value;
PlaceHolder(const PlaceHolder &placeHolder) : value(placeHolder.value) {}
PlaceHolder(const T& value) : value(value) {}
};
Can I make any assumptions/assertions about the memory layout of any
particular PlaceHolder<X,Y> struct?
In particular, is the following function valid, or is it UB?
template <typename T, unsigned size>
T &get(PlaceHolder<T, size> &place, int index) {
return reinterpret_cast<T*>(&place)[index];
}