So if I do something like this:
#include <ios>
using std::forward;
template<class T>
struct pod_version final{
private:
alignas(T) uint8_t m_data[sizeof(T)];
public:
pod_version()=default;
pod_version(const pod_version&)=default;
pod_version(pod_version&&)=default;
~pod_version()=default;
pod_version& operator=(const pod_version&)=default;
pod_version& operator=(pod_version&&)=default;
template<class...V>void construct(V&&...v){
new (m_data) T(forward<V>(v)...);
}
void destroy(){
reinterpret_cast<T*>(m_data)->~T(); // g++: warning typed punned blah im allowed to break ur code LOL
reinterpret_cast<T*>(this)->~T(); // g++: :D
}
};
int main(){
pod_version<int> x;
x.construct(5);
x.destroy();
return 0;
}
Note: "m_data" and "this" should point to the same place...
gcc 4.8.1
Use char
, not uint8_t
.
The type punning rules have a special case for char
, signed char
, and unsigned char
, and ONLY these types. uint8_t
is not required to mean one of them.
Note that the whole concept of this pod_version
class is suspect. You're forcing trivial copy semantics on types for which they are not valid. Your code will attempt to call a destructor on uninitialized memory, or on a binary image of an object. Both will result in undefined behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With