Compared to calling new() directly, using a Pointer-returning Make()
reduces memory leaks related to freshly allocated heap objects. Perfect
forwarding of constructor parameters through Make() minimizes overhead.
const auto foo1 = new Foo(...); // XXX: leak-prone
const auto foo2 = Foo::Pointer::Make(...); // OK
Future changes will prohibit RefCountable object creation via direct
new() calls and require using Make() or an equivalent safety API.
#include "base/Lock.h"
#include <iostream>
+#include <utility>
/**
* Template for Reference Counting pointers.
{
public:
+ /// creates a new C object using given C constructor arguments (if any)
+ /// \returns a refcounting pointer to the created object
+ template<typename... Args>
+ inline static auto Make(Args&&... args) {
+ return RefCount<C>(new C(std::forward<Args>(args)...));
+ }
RefCount () : p_ (nullptr) {}
RefCount (C const *p) : p_(p) { reference (*this); }