| 1 |
.. title:: clang-tidy - modernize-replace-disallow-copy-and-assign-macro |
| 2 |
|
| 3 |
modernize-replace-disallow-copy-and-assign-macro |
| 4 |
================================================ |
| 5 |
|
| 6 |
Finds macro expansions of ``DISALLOW_COPY_AND_ASSIGN(Type)`` and replaces them |
| 7 |
with a deleted copy constructor and a deleted assignment operator. |
| 8 |
|
| 9 |
Before the ``delete`` keyword was introduced in C++11 it was common practice to |
| 10 |
declare a copy constructor and an assignment operator as private members. This |
| 11 |
effectively makes them unusable to the public API of a class. |
| 12 |
|
| 13 |
With the advent of the ``delete`` keyword in C++11 we can abandon the |
| 14 |
``private`` access of the copy constructor and the assignment operator and |
| 15 |
delete the methods entirely. |
| 16 |
|
| 17 |
When running this check on a code like this: |
| 18 |
|
| 19 |
.. code-block:: c++ |
| 20 |
|
| 21 |
class Foo { |
| 22 |
private: |
| 23 |
DISALLOW_COPY_AND_ASSIGN(Foo); |
| 24 |
}; |
| 25 |
|
| 26 |
It will be transformed to this: |
| 27 |
|
| 28 |
.. code-block:: c++ |
| 29 |
|
| 30 |
class Foo { |
| 31 |
private: |
| 32 |
Foo(const Foo &) = delete; |
| 33 |
const Foo &operator=(const Foo &) = delete; |
| 34 |
}; |
| 35 |
|
| 36 |
Known Limitations |
| 37 |
----------------- |
| 38 |
|
| 39 |
* Notice that the migration example above leaves the ``private`` access |
| 40 |
specification untouched. You might want to run the check :doc:`modernize-use-equals-delete |
| 41 |
<../modernize/use-equals-delete>` to get warnings for deleted functions in |
| 42 |
private sections. |
| 43 |
|
| 44 |
Options |
| 45 |
------- |
| 46 |
|
| 47 |
.. option:: MacroName |
| 48 |
|
| 49 |
A string specifying the macro name whose expansion will be replaced. |
| 50 |
Default is `DISALLOW_COPY_AND_ASSIGN`. |
| 51 |
|
| 52 |
See: https://en.cppreference.com/w/cpp/language/function#Deleted_functions |