Click here to Skip to main content
13,900,622 members
Click here to Skip to main content
Add your own
alternative version

Stats

1.1K views
Posted 15 Mar 2019
Licenced MIT

Refactoring std::auto_ptr

, 15 Mar 2019
Rate this:
Please Sign up or sign in to vote.
How to refactor std::auto_ptr

A colleague at work was recently tasked with refactoring some legacy code and came across the good old std::auto_ptr. In C++0x, it has been deprecated (and later removed outright; I can’t even compile std::auto_ptr code in Visual Studio 2017 nor Xcode 10.1) so it was his job to rewrite it in the modern C++ way. The code that my colleague came across had another problem. Can you spot it?

std::auto_ptr<T> p1(new T[3]);

std::auto_ptr was never meant to hold pointers to arrays. So the code above, assuming it didn’t crash, was leaking memory (regardless of what it holds, std::auto_ptr always calls delete in its destructor; delete[] was needed in the code above).

So how do we refactor legacy std::auto_ptr code into modern C++? The answer is std::unique_ptr (https://en.cppreference.com/w/cpp/memory/unique_ptr). A std::auto_ptr holding a pointer to a single object of type T, in modern C++, becomes:

auto p2 = std::make_unique<T>();

std::make_unique can forward constructor parameters to T::T, like this:

auto p3 = std::make_unique<T>("string parameter");

And an array of Ts of size N becomes:

auto p4 = std::make_unique<T[]>(N);

Note that for std::make_unique to be able to create an array of Ts, T must have a T::T() (default constructor; or a constructor with all parameters having default values: T::T(int x = 0)).

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Martin Vorbrodt
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
Suggestionshared_ptr Pin
Stefan_Lang20-Mar-19 22:27
memberStefan_Lang20-Mar-19 22:27 
Questionmake_unique Pin
Stefan_Lang20-Mar-19 22:26
memberStefan_Lang20-Mar-19 22:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 15 Mar 2019
Article Copyright 2019 by Martin Vorbrodt
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid