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

nostd::shared_ptr does not allow conversion from std::unique_ptr #3101

Closed
t-b opened this issue Oct 16, 2024 · 2 comments
Closed

nostd::shared_ptr does not allow conversion from std::unique_ptr #3101

t-b opened this issue Oct 16, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@t-b
Copy link
Contributor

t-b commented Oct 16, 2024

Linux: debian bookworm, GCC 12.2.0

The nostd::shared_ptr class does not support creation from a std::unique_ptr whereas std::shared_ptr supports that. What missing is a constructor and assignment operator, see (13) at https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr and (6) at https://en.cppreference.com/w/cpp/memory/shared_ptr/operator%3D.

This is an issue for my use case since v1.17.0 as the return type of e.g. std::unique_ptr<opentelemetry::sdk::logs::LoggerProvider> LoggerProviderFactory::Create(...) changed. My fix is to introduce

template <typename T>
opentelemetry::nostd::shared_ptr<T> StdUniqueToNostdShared(std::unique_ptr<T> &&other)
{
    return opentelemetry::nostd::shared_ptr<T>{std::shared_ptr<T>{other.release()}};
}

and the use it like

        auto logger_provider = StdUniqueToNostdShared(
            opentelemetry::sdk::logs::LoggerProviderFactory::Create(std::move(processor), resource));

        opentelemetry::logs::Provider::SetLoggerProvider(interface->logger_provider);

or am I doing something wrong?

@t-b t-b added the bug Something isn't working label Oct 16, 2024
@github-actions github-actions bot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Oct 16, 2024
@owent
Copy link
Member

owent commented Oct 17, 2024

nostd::shared_ptr has constructor for std::unique_ptr<T> &&other, so it can be constructed with the rvalue of std::unique_ptr<T>, just like std::shared_ptr.
Could you please provide the codes that can not compile? I think the problem for nostd::shared_ptr mey be it should be allow to be constructed by not only std::unique_ptr<T>, but also std::unique_ptr<T, Dtor>.

@t-b
Copy link
Contributor Author

t-b commented Oct 17, 2024

I had a look again at one of the examples (examples/simple/main.cc). And that shows how this can be done without any problems. The basic idea is that you create a std::shared_ptr already from the return object of Create. And from there it just works.

Thanks for your time.

@t-b t-b closed this as completed Oct 17, 2024
@marcalff marcalff removed the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants