std::auto_ptr or boost::shared_ptr for pImpl idiom?

后端 未结 9 1383
醉梦人生
醉梦人生 2020-12-23 15:04

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

9条回答
  •  天涯浪人
    2020-12-23 15:52

    If you are being really pedantic there is no absolute guarantee that using an auto_ptr member does not require a full definition of the auto_ptr's template parameter at the point at which it is used. Having said that, I've never seen this not work.

    One variation is to use a const auto_ptr. This works so long as you can construct your 'pimpl' with a new expression inside the initialiser list and guarantees that the compiler cannot generate default copy constructor and assignment methods. A non-inline destructor for the enclosing class still needs to be provided.

    Other things being equal, I would favour an implementation that uses just the standard libraries as it keeps things more portable.

提交回复
热议问题