I was thinking about some memory pools / allocation materials that I could write, so I came up with this overload operator newthat I want to use to facilitate memory reuse. I am wondering if there are any problems you guys can come up with with my implementation (or any other possible ones).
#include <cstddef>
namespace ns {
struct renew_t { };
renew_t const renew;
}
template<typename T>
inline void * operator new(std::size_t size, T * p, ns::renew_t renew_constant) {
p->~T();
return p;
}
template<typename T>
inline void operator delete(void *, T *, ns::renew_t renew_constant) { }
It can be used as
int main() {
foo * p(new foo());
new(p, ns::renew) foo(42);
delete p;
}
source
share