remove _BOX_CPP directive support as it causes undefined behaviour on stack popping

lib
Avril 3 years ago
parent 2bab6235db
commit 7e9f2083be
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -31,7 +31,8 @@
#ifdef __cplusplus #ifdef __cplusplus
#ifdef _BOX_CPP #ifdef _BOX_CPP
#warning "Using BOX with non-POD types is discouraged" #include <utility>
#error "Using BOX with non-POD types causes memory leaks"
#endif #endif
#include <cstdlib> #include <cstdlib>
@ -52,11 +53,23 @@ inline T* box()
return (T*)ptr; return (T*)ptr;
} }
#ifdef _BOX_CPP
template<typename T>
inline T* box(T&& val)
{
return new T(std::move(val));
}
#endif
template<typename T> template<typename T>
inline T* box(T val) inline T* box(T val)
{ {
#ifdef _BOX_CPP
T* ptr = new T(std::move(val));
#else
T* ptr = box<T>(); T* ptr = box<T>();
*ptr = val; *ptr = val;
#endif
return ptr; return ptr;
} }

Loading…
Cancel
Save