From 6ae8dc62c7bdaf8f8f701dd6cbef4f289d429c00 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Thu, 7 Jan 2016 00:38:20 +0100 Subject: [PATCH] supplement: a "Make P" free function for our smart-ptr the usual drill... only when wrapped into a factory function, RAII is really airtight, even when used from within expression evaluation. Thanks C++11 we're now able to provide such en passant --- research/try.cpp | 9 +-------- src/lib/p.hpp | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/research/try.cpp b/research/try.cpp index 3fd2439b2..89a9c7d3e 100644 --- a/research/try.cpp +++ b/research/try.cpp @@ -80,6 +80,7 @@ typedef unsigned int uint; #include using lib::P; +using lib::newP; using lib::diff::GenNode; using lib::meta::is_basically; using lib::meta::is_StringLike; @@ -103,14 +104,6 @@ class Reticent }; -template -inline P -newP (ARGS&&... ctorArgs) -{ - return P{new X {std::forward(ctorArgs)...}}; -} - - template using BasicallyString = is_basically; diff --git a/src/lib/p.hpp b/src/lib/p.hpp index 01d5c69c6..ef757f62e 100644 --- a/src/lib/p.hpp +++ b/src/lib/p.hpp @@ -133,6 +133,22 @@ namespace lib { + + /** Helper to create and manage by lib::P + * @tparam X the type of the new object to create on the heap + * @param ctorArgs arbitrary arguments to pass to ctor of `X` + * @return managing smart-ptr of type P, holding onto the + * object just created on the heap. + */ + template + inline P + newP (ARGS&&... ctorArgs) + { + return P{new X {std::forward(ctorArgs)...}}; + } + + + /** * use custom string conversion on pointee, if applicable, * otherwise fall back to a human readable type string.S @@ -141,7 +157,7 @@ namespace lib { inline P::operator std::string() const noexcept try { - if (this->get()) + if (BASE::get()) return meta::CustomStringConv::invoke (this->operator*()); else return "⟂ P<"+meta::typeStr(this->get())+">";