shared_ptr Derived types cannot be implicitly cast to the share_ptr Base types with gsl::not_null
Open
@gdr-at-ms is already working on this.
Since Jan 23, 2026.
Status: Blocked
Status: Open
Type: Bug
- Dominant language
- C++
- Stars
- 6.7k
- Forks
- 772
- Avg merge
- 4d 23h
- Merged PRs (30d)
- 9
Description
Describe the bug
When I try to change function A to return a gsl::not_null<std::shared_ptr> instead of std::shared_ptr, it causes problems in function B that returns the result of function A as a std::shared_ptr
To Reproduce
#include <gsl>
#include <memory>
class Base
{
};
class Derived : public Base
{
};
gsl::not_null<std::shared_ptr<Derived>> A()
{
return std::make_shared<Derived>();
}
std::shared_ptr<Base> B()
{
// this fails to compile after the result of A was changed to a not_null
return A();
}
// your repro here: ...
Expected behavior
I expect the code above to compile and work exactly the same with or without the gsl::not_null
Spec (please complete the following information):
- OS: Windows
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29335 for x64
All C++ versions
Additional context
Changing the implicit conversion operator:
constexpr operator T() const { return get(); }
To a templatized version seems to fix this issue:
template <typename RESULT_TYPE,
typename = std::enable_if_t<
std::is_convertible<T, RESULT_TYPE>::value>>
operator RESULT_TYPE() const
{
return get();
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.