Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8631983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:17:27+00:00 2026-06-12T09:17:27+00:00

On the boost.org website, I saw an example of preventing delete px.get() for a

  • 0

On the boost.org website, I saw an example of preventing delete px.get() for a shared_ptr (http://www.boost.org/doc/libs/1_51_0/libs/smart_ptr/sp_techniques.html#preventing_delete).

This is a good technique and I would like to apply this using std::unique_ptr in C++11, and after tooling around for a bit, I can’t quite get their example working with std::unique_ptr.

Is it possible to prevent delete px.get() from being called on a std::unique_ptr?

Here is the code from the boost.org website showing how to prevent delete px.get from being called:

class X
{
private:

    ~X();

    class deleter;
    friend class deleter;

    class deleter
    {
    public:

        void operator()(X * p) { delete p; }
    };

public:

    static shared_ptr<X> create()
    {
        shared_ptr<X> px(new X, X::deleter());
        return px;
    }
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:17:28+00:00Added an answer on June 12, 2026 at 9:17 am

    The idea remains the same for unique_ptr also, except for the fact that the type of the deleter is part of the unique_ptr‘s type.

    #include <functional>
    #include <memory>
    #include <iostream>
    
    using namespace std;
    
    class X
    {
    private:
    
      ~X() {}
    
      class deleter
      {
      public:
    
        void operator()(X * p) { delete p; }
      };
      friend class deleter;
    
    public:
    
      static shared_ptr<X> create_shared()
      {
        shared_ptr<X> px(new X, X::deleter());
        return px;
      }
    
      static unique_ptr<X, void(*)(X*)> create_unique()
      {
        return unique_ptr<X, void(*)(X*)>( new X, []( X *x ) { 
                                                      X::deleter()( x ); 
                                                    } );
      }
    
      // If using VS2010
      static unique_ptr<X, std::function<void(X*)>> create_unique()
      {
        return unique_ptr<X, std::function<void(X*)>>( new X, X::deleter() );
      }
    };
    
    int main()
    {
      auto x = X::create_shared();
      auto y = X::create_unique();
    }
    

    VS2010 does not implement implicit conversion of capture-less lambda to a function pointer, so the first create_unique will not work on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this example class http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/sp_techniques.html#as_lock and I'm struggling with the syntax. class
the example code on the boost website is not working. http://www.boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/tutorial.html#Using-path-decomposition int main(int argc,
I found http://www.boost.org/doc/libs/1_49_0/libs/graph/example/incremental_components.cpp and want to check if it will work for me. How
I'am trying to understand the example from program_options of the boost library ( http://www.boost.org/doc/libs/1_38_0/doc/html/program_options/tutorial.html#id3761458
I'm trying to build an example of boost::asio http://www.boost.org/doc/libs/1_43_0/doc/html/boost_asio/example/echo/async_tcp_echo_server.cpp but without any luck. System:
I have the following code (which largely follows the first example here: http://www.boost.org/doc/libs/1_42_0/libs/multi_index/doc/examples.html )).
http://www.boost.org/doc/libs/1_35_0/doc/html/boost/get_id405862.html template<typename U, typename T1, typename T2, ..., typename TN> U & get(variant<T1, T2,
The example I'm trying to compile is from: http://www.boost.org/doc/libs/1_46_1/doc/html/boost_asio/examples.html (the chat example) Here is
I'm trying to follow this example http://www.boost.org/doc/libs/1_42_0/libs/serialization/doc/serialization.html#constructors but I keep getting errors. Following the
I'm trying to compile and run the following example on Ubuntu 10.10 http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/example/http/server3/ but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.