From fcdff2f2053282b56091000c98571d22161933f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20Garc=C3=ADa=20Salas?= Date: Sat, 14 Nov 2015 03:05:59 +0100 Subject: [PATCH] Add static_pointer_cast The current implementation of gsl::not_null provides a constructor to convert from not_null to not_null when there exists an implicit conversion from U to T. However it does not provide an easy way to convert from not_null to not_null when the conversion from U to T requires the use of static_cast. Manually, one would have to do: auto converted = gsl::not_null(static_cast(original.get())) In accordance with the STL naming conventions, I propose the function "gsl::static_pointer_cast(gsl::not_null const &)" to do the job. Until the GSL gets merged with the STL, it would lie on the ::gsl namespace, instead of being on ::std along with static_pointer_cast(std::shared_ptr const &). Note 1: We could add noexcept if not_null's constructor were noexcept. Note 2: dynamic_pointer_cast should not be implemented, since dynamic_cast can return nullptr. --- include/gsl.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/include/gsl.h b/include/gsl.h index e20ac69..745a078 100644 --- a/include/gsl.h +++ b/include/gsl.h @@ -195,6 +195,8 @@ private: not_null& operator-=(size_t) = delete; }; +template not_null static_pointer_cast(not_null const & ptr) { return not_null{static_cast(ptr.get())}; } + } // namespace gsl namespace std