Is it possible to construct a pointer instead of shared_ptr?

Issue #1 wontfix
Former user created an issue

Could we construct a pointer instead of shared_ptr directly? We need a unique_ptr actually, but as it's forbidden to passed out by dicpp, create by raw pointer may be a workaround.

Comments (6)

  1. Sohail Somani repo owner

    What you're looking for is the ability to allocate memory that is unmanaged by dicpp but use all of the other niceness.

    So one option is the following:

    1. Create a new provider (unmanaged) that returns a shared_ptr with a null deleter
    2. Patch the code to add an overload to cast from shared_ptr to unique_ptr. I would also put in a check to ensure that anything constructed into a unique_ptr is not managed here
    3. When you register all the types that should be unmanaged, use the unmanaged provider: r.add(r.type<Whatever>().provider<unmanaged>())

    Then you can use std::unique_ptr as a parameter for your constructors.

    Let me know if you have any problems with this.

  2. 王宇鹏

    I'm sorry, I couldn't find an example, and based on my limited knowledge of C++, I don't know how to implement 1&2-_-(I do tried...) Would you mind provide an example please? Great thanks!

  3. Sohail Somani repo owner

    If your knowledge of C++ is limited then I suggest you not go down this path for now.

  4. Log in to comment