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 8140529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:11:34+00:00 2026-06-06T12:11:34+00:00

Does Boost, or anything else, contain a container will act like a shared pointer

  • 0

Does Boost, or anything else, contain a container will act like a shared pointer but allow me to control what happens to the shared ‘resource’ at the end of it’s life? I want to encapsulate an object that can be handed around but, when no longer needed can be closed in a context defined way.

For example, I might want to create and pass around a file handle knowing that when it goes out of scope, the file will be closed automatically, but I don’t want to delete the handle.

I could implement it myself, but would rather not get into that if the framework already exists – someone has no doubt done it better. I can’t use boost::shared_ptr, at least not in it’s normal form, as the resource should not be deleted at end of life.

  • 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-06T12:11:36+00:00Added an answer on June 6, 2026 at 12:11 pm

    Are you aware that std::shared_ptr can take a custom deleter class? This need not actually use “delete” or “free”, but could easily use some other sort of mechanism (such as a reference counting mechanism’s release and so on).

    Here’s a dead simple example for you:

    std::shared_ptr<FILE> foo(fopen("la", "r"), fclose);
    

    The deleter just needs to be a function that takes the pointer type that the shared_ptr wraps. In this case, whenfoo goes out of scope, shared_ptr will close the file for you. (Note: this isn’t a totally sensible implementation, because no error values are checked. It is just an example).

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

Sidebar

Related Questions

Why does the following code not allow foo(ptr) to be called ? #include <boost/scoped_ptr.hpp>
Does C++ standard library and/or Boost have anything similar to the filter function found
Does Boost Ranges have a built-in way to cout the elements easily, for example
How does one use boost::spirit with an input that consists of something other than
Does calling a destructor of an boost::lock object explicitly have any consequence at all?
The following does not compile: class Foo { public: Foo( boost::shared_ptr< Bar > arg
What is the purpose of using the Boost.MPI library? Does it increase performance? And
I have a function called PreProcessSource, which allocates a boost::wave::context and does some preprocessing;
What does boost::thread do if it fails to create the thread? The winAPI returns
The following code fragment does nothing, but illustrates the problem. It was extracted from

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.