所以如果我做这样的事情:
#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;
}
注意:“m_data”和“this”应该指向同一个地方...
GCC 4.8.1