I need to create a constexpr array from bytes from a constexpr struct.
I tried to extract it from union:
template<typename T> union U { T o; std::array<uint8_t, sizeof(T)> d; }; template<typename T> constexpr std::array<uint8_t, sizeof(T)> o2ba(const T o) { return U<T>{o}.d; }
but it is not suitable for gcc and msvc compilers to access d instead of the initialized member o. It works when initializing a non constexpr object, as shown below.
int main() { constexpr A x{ 1 }; auto y = o2ba(x);
But that is not what I need. Is there any way to do this?
source share