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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:47:41+00:00 2026-05-10T20:47:41+00:00

When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of

  • 0

When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of a std::auto_ptr? I’m sure I once read that the boost version is more exception friendly?

class Foo { public:     Foo(); private:     struct impl;     std::auto_ptr<impl> impl_; };  class Foo { public:     Foo(); private:     struct impl;     boost::shared_ptr<impl> impl_; }; 

[EDIT] Is it always safe to use std::auto_ptr<> or are there situations when an alternative boost smart pointer is required?

  • 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. 2026-05-10T20:47:42+00:00Added an answer on May 10, 2026 at 8:47 pm

    You shouldn’t really use std::auto_ptr for this. The destructor won’t be visible at the point you declare the std::auto_ptr, so it might not be called properly. This is assuming that you are forward declaring your pImpl class, and creating the instance inside the constructor in another file.

    If you use boost::scoped_ptr (no need for shared_ptr here, you won’t be sharing the pimpl with any other objects, and this is enforced by scoped_ptr being noncopyable), you only need the pimpl destructor visible at the point you call the scoped_ptr constructor.

    E.g.

    // in MyClass.h  class Pimpl;  class MyClass  {  private:     std::auto_ptr<Pimpl> pimpl;  public:      MyClass(); };  // Body of these functions in MyClass.cpp 

    Here, the compiler will generate the destructor of MyClass. Which must call auto_ptr’s destructor. At the point where the auto_ptr destructor is instantiated, Pimpl is an incomplete type. So in to the auto_ptr destructor when it deletes the the Pimpl object, it won’t know how to call the Pimpl destructor.

    boost::scoped_ptr (and shared_ptr) don’t have this problem, because when you call the constructor of a scoped_ptr (or the reset method) it also makes a function-pointer-equivalent that it will use instead of calling delete. The key point here is that it instantiates the deallocation function when Pimpl is not an incomplete type. As a side note, shared_ptr allows you to specify a custom deallocation function, so you can use for it for things like GDI handles or whatever else you may want – but that’s overkill for your needs here.

    If you really want to use std::auto_ptr, then you need to take extra care by making sure you define your MyClass destructor in MyClass.cpp when Pimpl is fully defined.

    // in MyClass.h  class Pimpl;  class MyClass  {  private:     std::auto_ptr<Pimpl> pimpl;  public:      MyClass();     ~MyClass(); }; 

    and

    // in MyClass.cpp  #include 'Pimpl.h'  MyClass::MyClass() : pimpl(new Pimpl(blah)) { }  MyClass::~MyClass()  {     // this needs to be here, even when empty } 

    The compiler will generate the code destruct all of the MyClass members effectively ‘in’ the empty destructor. So at the point the auto_ptr destructor is instantiated, Pimpl is no longer incomplete and the compiler now knows how to call the destructor.

    Personally, I don’t think it’s worth the hassle of making sure everything is correct. There’s also the risk that somebody will come along later and tidy up the code by removing the seemingly redundant destructor. So it’s just safer all round to go with boost::scoped_ptr for this kind of thing.

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

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The logarithm of the factorial can be used to calculate… May 11, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer From the class that extends WebView: public void selectAndCopyText() {… May 11, 2026 at 11:36 pm
  • Editorial Team
    Editorial Team added an answer Try this: Select date, associate_id, count(*) as per_date From tracking… May 11, 2026 at 11:36 pm

Related Questions

I'm developing an application that needs to interact over FTP. For this communication I
When using the app_offline.htm feature of ASP.NET, it only allows html, but no images.
When using the php include function the include is succesfully executed, but it is
When using the Eclipse->Team->Show History view, what's the difference between using Subclipse Get Contents

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.