Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shared_ptr Derived types cannot be implicitly cast to the share_ptr Base types with gsl::not_null #1187

Open
daltairwalter opened this issue Jan 27, 2025 · 2 comments
Assignees
Labels
Status: Blocked Cannot proceed due to external factors Status: Open Needs attention Type: Bug Indicates a bug or error

Comments

@daltairwalter
Copy link

daltairwalter commented Jan 27, 2025

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();
    }
@carsonRadtke
Copy link
Collaborator

Thanks for reporting this issue!

We will look into improving the experience here - the decision on how to proceed will likely be made at the internal maintainer's sync.

@carsonRadtke
Copy link
Collaborator

Maintainer's call: Forward this issue to CppCoreGuidelines folks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Blocked Cannot proceed due to external factors Status: Open Needs attention Type: Bug Indicates a bug or error
Projects
Status: New
Development

No branches or pull requests

2 participants