1.. title:: clang-tidy - bugprone-forwarding-reference-overload
2
3bugprone-forwarding-reference-overload
4======================================
5
6The check looks for perfect forwarding constructors that can hide copy or move
7constructors. If a non const lvalue reference is passed to the constructor, the
8forwarding reference parameter will be a better match than the const reference
9parameter of the copy constructor, so the perfect forwarding constructor will be
10called, which can be confusing.
11For detailed description of this issue see: Scott Meyers, Effective Modern C++,
12Item 26.
13
14Consider the following example:
15
16.. code-block:: c++
17
18    class Person {
19    public:
20      // C1: perfect forwarding ctor
21      template<typename T>
22      explicit Person(T&& n) {}
23
24      // C2: perfect forwarding ctor with parameter default value
25      template<typename T>
26      explicit Person(T&& n, int x = 1) {}
27
28      // C3: perfect forwarding ctor guarded with enable_if
29      template<typename T, typename X = enable_if_t<is_special<T>,void>>
30      explicit Person(T&& n) {}
31
32      // (possibly compiler generated) copy ctor
33      Person(const Person& rhs);
34    };
35
36The check warns for constructors C1 and C2, because those can hide copy and move
37constructors. We suppress warnings if the copy and the move constructors are both
38disabled (deleted or private), because there is nothing the perfect forwarding
39constructor could hide in this case. We also suppress warnings for constructors
40like C3 that are guarded with an ``enable_if``, assuming the programmer was aware of
41the possible hiding.
42
43Background
44----------
45
46For deciding whether a constructor is guarded with enable_if, we consider the
47default values of the type parameters and the types of the constructor
48parameters. If any part of these types is ``std::enable_if`` or ``std::enable_if_t``,
49we assume the constructor is guarded.
50