0

Have shared project library that has to be compiled with various compilers C++17 C++03 etc. So just using the better unique_ptr or the less than perfect auto_ptr as appropriate is not ideal if the code is to be kept common.

Have look at the #ifdef type solution but it gets messy. Plus making edits to well tried and tested coded is not a particular attractive idea!

Anyone know of a form, fit and function drop in replacement for auto_ptr?

valiano
  • 16,433
  • 7
  • 64
  • 79
Brian M
  • 1
  • 1
  • 2
    One thing you could do is you could use `unique_ptr` if you have C++11 or greater, or `shared_ptr` before (even `boost::shared_ptr` if there's no `std::shared_ptr`). That would avoid the problems with `auto_ptr` – Justin Jul 06 '17 at 21:55
  • If it's only internal to the library, you could use have an #ifdef that resolves to `auto_ptr` for c++03 and `unique_ptr` for c++11 and later, and another that's blank or `std::move`, for ownership transfers. If its external you can't be sure that the user won't misuse `auto_ptr` – Caleth Jul 06 '17 at 22:01
  • BTW, the OP doesn't seem interested in a solution with macros. – Cosme Jul 07 '17 at 02:30

1 Answers1

1

There are Boost.Move and Boost Smart Pointer. std::unique_ptr and std::auto_ptr may be replaced by boost::movelib::unique_ptr. std::shared_ptr and std::weak_ptr by boost::shared_ptr and boost::weak_ptr.

And, you can be more expressive with boost::scoped_ptr when there is no intent to transfer ownership.

Cosme
  • 191
  • 2
  • 6