| 1 |
.. title:: clang-tidy - cppcoreguidelines-special-member-functions |
| 2 |
|
| 3 |
cppcoreguidelines-special-member-functions |
| 4 |
========================================== |
| 5 |
|
| 6 |
The check finds classes where some but not all of the special member functions |
| 7 |
are defined. |
| 8 |
|
| 9 |
By default the compiler defines a copy constructor, copy assignment operator, |
| 10 |
move constructor, move assignment operator and destructor. The default can be |
| 11 |
suppressed by explicit user-definitions. The relationship between which |
| 12 |
functions will be suppressed by definitions of other functions is complicated |
| 13 |
and it is advised that all five are defaulted or explicitly defined. |
| 14 |
|
| 15 |
Note that defining a function with ``= delete`` is considered to be a |
| 16 |
definition. |
| 17 |
|
| 18 |
This check implements `C.21 |
| 19 |
<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-five>`_ |
| 20 |
from the C++ Core Guidelines. |
| 21 |
|
| 22 |
Options |
| 23 |
------- |
| 24 |
|
| 25 |
.. option:: AllowSoleDefaultDtor |
| 26 |
|
| 27 |
When set to `true` (default is `false`), this check will only trigger on |
| 28 |
destructors if they are defined and not defaulted. |
| 29 |
|
| 30 |
.. code-block:: c++ |
| 31 |
|
| 32 |
struct A { // This is fine. |
| 33 |
virtual ~A() = default; |
| 34 |
}; |
| 35 |
|
| 36 |
struct B { // This is not fine. |
| 37 |
~B() {} |
| 38 |
}; |
| 39 |
|
| 40 |
struct C { |
| 41 |
// This is not checked, because the destructor might be defaulted in |
| 42 |
// another translation unit. |
| 43 |
~C(); |
| 44 |
}; |
| 45 |
|
| 46 |
.. option:: AllowMissingMoveFunctions |
| 47 |
|
| 48 |
When set to `true` (default is `false`), this check doesn't flag classes |
| 49 |
which define no move operations at all. It still flags classes which define |
| 50 |
only one of either move constructor or move assignment operator. With this |
| 51 |
option enabled, the following class won't be flagged: |
| 52 |
|
| 53 |
.. code-block:: c++ |
| 54 |
|
| 55 |
struct A { |
| 56 |
A(const A&); |
| 57 |
A& operator=(const A&); |
| 58 |
~A(); |
| 59 |
}; |
| 60 |
|
| 61 |
.. option:: AllowMissingMoveFunctionsWhenCopyIsDeleted |
| 62 |
|
| 63 |
When set to `true` (default is `false`), this check doesn't flag classes |
| 64 |
which define deleted copy operations but don't define move operations. This |
| 65 |
flag is related to Google C++ Style Guide `Copyable and Movable Types |
| 66 |
<https://google.github.io/styleguide/cppguide.html#Copyable_Movable_Types>`_. |
| 67 |
With this option enabled, the following class won't be flagged: |
| 68 |
|
| 69 |
.. code-block:: c++ |
| 70 |
|
| 71 |
struct A { |
| 72 |
A(const A&) = delete; |
| 73 |
A& operator=(const A&) = delete; |
| 74 |
~A(); |
| 75 |
}; |
| 76 |
|
| 77 |
.. option:: AllowImplicitlyDeletedCopyOrMove |
| 78 |
|
| 79 |
When set to `true` (default is `false`), this check doesn't flag classes |
| 80 |
which implicitly delete copy or move operations. |
| 81 |
With this option enabled, the following class won't be flagged: |
| 82 |
|
| 83 |
.. code-block:: c++ |
| 84 |
|
| 85 |
struct A : boost::noncopyable { |
| 86 |
~A() { std::cout << "dtor\n"; } |
| 87 |
}; |