The current implementation of gsl::not_null provides a constructor to
convert from not_null<U> to not_null<T> when there exists an implicit
conversion from U to T.
However it does not provide an easy way to convert from not_null<U> to
not_null<T> when the conversion from U to T requires the use of
static_cast.
Manually, one would have to do:
auto converted = gsl::not_null<T>(static_cast<T>(original.get()))
In accordance with the STL naming conventions, I propose the function
"gsl::static_pointer_cast(gsl::not_null<U> 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<U> 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.
Also removed unused constant member variable that seemed to be there
to prevent maybe_null_* being used with anything other than a pointer,
which is being taken care of with a static_assert now.
for eg consider this case
[code]
string value = "someVeryLongErrorMessageIAm";
finally([value] { PrintErrorMessage(value); }
[/code]
With the current changes before the call to PrintErrorMessage there will be 3 calls to copy constructor for string(1 when it's captured in closure, 2nd when finally is called and 3rd when it's passed to Final_act . With my patch there will be 1 call to the copy constructor and 2 to the move constructor for the scenario in example, so 2 potential deep copies will be saved for some objects.
Validated that code builds from root, and all tests pass after my change. Also validated that indeed copy constructor calls are saved for objects that support move semantics.