From 7e9f2083bea7f89a127598699a7e64038cc1166c Mon Sep 17 00:00:00 2001 From: Avril Date: Thu, 18 Mar 2021 18:43:26 +0000 Subject: [PATCH] remove _BOX_CPP directive support as it causes undefined behaviour on stack popping --- include/box.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/include/box.h b/include/box.h index c69a6c5..1e25553 100644 --- a/include/box.h +++ b/include/box.h @@ -31,7 +31,8 @@ #ifdef __cplusplus #ifdef _BOX_CPP -#warning "Using BOX with non-POD types is discouraged" +#include +#error "Using BOX with non-POD types causes memory leaks" #endif #include @@ -52,11 +53,23 @@ inline T* box() return (T*)ptr; } +#ifdef _BOX_CPP +template +inline T* box(T&& val) +{ + return new T(std::move(val)); +} +#endif + template inline T* box(T val) { +#ifdef _BOX_CPP + T* ptr = new T(std::move(val)); +#else T* ptr = box(); *ptr = val; +#endif return ptr; }