brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · 8a8c30e Raw
82 lines · plain
1.. title:: clang-tidy - modernize-replace-auto-ptr2 3modernize-replace-auto-ptr4==========================5 6This check replaces the uses of the deprecated class ``std::auto_ptr`` by7``std::unique_ptr`` (introduced in C++11). The transfer of ownership, done8by the copy-constructor and the assignment operator, is changed to match9``std::unique_ptr`` usage by using explicit calls to ``std::move()``.10 11Migration example:12 13.. code-block:: c++14 15  -void take_ownership_fn(std::auto_ptr<int> int_ptr);16  +void take_ownership_fn(std::unique_ptr<int> int_ptr);17 18   void f(int x) {19  -  std::auto_ptr<int> a(new int(x));20  -  std::auto_ptr<int> b;21  +  std::unique_ptr<int> a(new int(x));22  +  std::unique_ptr<int> b;23 24  -  b = a;25  -  take_ownership_fn(b);26  +  b = std::move(a);27  +  take_ownership_fn(std::move(b));28   }29 30Since ``std::move()`` is a library function declared in ``<utility>`` it may be31necessary to add this include. The check will add the include directive when32necessary.33 34 35Limitations36-----------37 38* If headers modification is not activated or if a header is not allowed to be39  changed this check will produce broken code (compilation error), where the40  headers' code will stay unchanged while the code using them will be changed.41 42* Client code that declares a reference to an ``std::auto_ptr`` coming from43  code that can't be migrated (such as a header coming from a 3\ :sup:`rd`44  party library) will produce a compilation error after migration. This is45  because the type of the reference will be changed to ``std::unique_ptr`` but46  the type returned by the library won't change, binding a reference to47  ``std::unique_ptr`` from an ``std::auto_ptr``. This pattern doesn't make much48  sense and usually ``std::auto_ptr`` are stored by value (otherwise what is49  the point in using them instead of a reference or a pointer?).50 51.. code-block:: c++52 53     // <3rd-party header...>54     std::auto_ptr<int> get_value();55     const std::auto_ptr<int> & get_ref();56 57     // <calling code (with migration)...>58    -std::auto_ptr<int> a(get_value());59    +std::unique_ptr<int> a(get_value()); // ok, unique_ptr constructed from auto_ptr60 61    -const std::auto_ptr<int> & p = get_ptr();62    +const std::unique_ptr<int> & p = get_ptr(); // won't compile63 64* Non-instantiated templates aren't modified.65 66.. code-block:: c++67 68     template <typename X>69     void f() {70         std::auto_ptr<X> p;71     }72 73     // only 'f<int>()' (or similar) will trigger the replacement.74 75Options76-------77 78.. option:: IncludeStyle79 80   A string specifying which include-style is used, `llvm` or `google`. Default81   is `llvm`.82