1 // RUN: %check_clang_tidy %s bugprone-forwarding-reference-overload %t
2 
3 namespace std {
4 template <bool B, class T = void> struct enable_if { typedef T type; };
5 
6 template <class T> struct enable_if<true, T> { typedef T type; };
7 
8 template <bool B, class T = void>
9 using enable_if_t = typename enable_if<B, T>::type;
10 
11 template <class T> struct enable_if_nice { typedef T type; };
12 } // namespace std
13 
14 namespace foo {
15 template <class T> struct enable_if { typedef T type; };
16 } // namespace foo
17 
18 template <typename T> constexpr bool just_true = true;
19 
20 class Test1 {
21 public:
22   template <typename T> Test1(T &&n);
23   // CHECK-NOTES: [[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the copy and move constructors [bugprone-forwarding-reference-overload]
24   // CHECK-NOTES: 48:3: note: copy constructor declared here
25   // CHECK-NOTES: 49:3: note: copy constructor declared here
26   // CHECK-NOTES: 50:3: note: move constructor declared here
27 
28   template <typename T> Test1(T &&n, int i = 5, ...);
29   // CHECK-NOTES: :[[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the copy and move constructors
30   // CHECK-NOTES: 48:3: note: copy constructor declared here
31   // CHECK-NOTES: 49:3: note: copy constructor declared here
32   // CHECK-NOTES: 50:3: note: move constructor declared here
33 
34   template <typename T, typename U = typename std::enable_if_nice<T>::type>
35   Test1(T &&n);
36   // CHECK-NOTES: :[[@LINE-1]]:3: warning: constructor accepting a forwarding reference can hide the copy and move constructors
37   // CHECK-NOTES: 48:3: note: copy constructor declared here
38   // CHECK-NOTES: 49:3: note: copy constructor declared here
39   // CHECK-NOTES: 50:3: note: move constructor declared here
40 
41   template <typename T>
42   Test1(T &&n, typename foo::enable_if<long>::type i = 5, ...);
43   // CHECK-NOTES: :[[@LINE-1]]:3: warning: constructor accepting a forwarding reference can hide the copy and move constructors
44   // CHECK-NOTES: 48:3: note: copy constructor declared here
45   // CHECK-NOTES: 49:3: note: copy constructor declared here
46   // CHECK-NOTES: 50:3: note: move constructor declared here
47 
Test1(const Test1 & other)48   Test1(const Test1 &other) {}
Test1(Test1 & other)49   Test1(Test1 &other) {}
Test1(Test1 && other)50   Test1(Test1 &&other) {}
51 };
52 
53 template <typename U> class Test2 {
54 public:
55   // Two parameters without default value, can't act as copy / move constructor.
56   template <typename T, class V> Test2(T &&n, V &&m, int i = 5, ...);
57 
58   // Guarded with enable_if.
59   template <typename T>
60   Test2(T &&n, int i = 5,
61         std::enable_if_t<sizeof(int) < sizeof(long), int> a = 5, ...);
62 
63   // Guarded with enable_if.
64   template <typename T, typename X = typename std::enable_if<
65                             sizeof(int) < sizeof(long), double>::type &>
66   Test2(T &&n);
67 
68   // Guarded with enable_if.
69   template <typename T>
70   Test2(T &&n, typename std::enable_if<just_true<T>>::type **a = nullptr);
71 
72   // Guarded with enable_if.
73   template <typename T, typename X = std::enable_if_t<just_true<T>> *&&>
74   Test2(T &&n, double d = 0.0);
75 
76   // Not a forwarding reference parameter.
77   template <typename T> Test2(const T &&n);
78 
79   // Not a forwarding reference parameter.
80   Test2(int &&x);
81 
82   // Two parameters without default value, can't act as copy / move constructor.
83   template <typename T> Test2(T &&n, int x);
84 
85   // Not a forwarding reference parameter.
86   template <typename T> Test2(U &&n);
87 };
88 
89 // The copy and move constructors are both disabled.
90 class Test3 {
91 public:
92   template <typename T> Test3(T &&n);
93 
94   template <typename T> Test3(T &&n, int I = 5, ...);
95 
96   Test3(const Test3 &rhs) = delete;
97 
98 private:
99   Test3(Test3 &&rhs);
100 };
101 
102 // Both the copy and the (compiler generated) move constructors can be hidden.
103 class Test4 {
104 public:
105   template <typename T> Test4(T &&n);
106   // CHECK-NOTES: :[[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the copy and move constructors
107 
108   Test4(const Test4 &rhs);
109   // CHECK-NOTES: :[[@LINE-1]]:3: note: copy constructor declared here
110 };
111 
112 // Nothing can be hidden, the copy constructor is implicitly deleted.
113 class Test5 {
114 public:
115   template <typename T> Test5(T &&n);
116 
117   Test5(Test5 &&rhs) = delete;
118 };
119 
120 // Only the move constructor can be hidden.
121 class Test6 {
122 public:
123   template <typename T> Test6(T &&n);
124   // CHECK-NOTES: :[[@LINE-1]]:25: warning: constructor accepting a forwarding reference can hide the move constructor
125 
126   Test6(Test6 &&rhs);
127   // CHECK-NOTES: :[[@LINE-1]]:3: note: move constructor declared here
128 private:
129   Test6(const Test6 &rhs);
130 };
131 
132 // Do not dereference a null BaseType.
133 template <class _Callable> class result_of;
134 template <class _Fp, class ..._Args> class result_of<_Fp(_Args...)> { };
135 template <class _Tp> using result_of_t = typename result_of<_Tp>::type;
136 
137 template <class... _Types> struct __overload;
138 template <class _Tp, class... _Types>
139 struct __overload<_Tp, _Types...> : __overload<_Types...> {
140   using __overload<_Types...>::operator();
141 };
142 
143 template <class _Tp, class... _Types>
144 using __best_match_t = typename result_of_t<__overload<_Types...>(_Tp&&)>::type;
145 
146 template <class... _Types>
147 class variant {
148 public:
149   template <class _Arg, class _Tp = __best_match_t<_Arg, _Types...> >
variant(_Arg && __arg)150   constexpr variant(_Arg&& __arg) {}
151   // CHECK-NOTES: :[[@LINE-1]]:13: warning: constructor accepting a forwarding reference can hide the copy and move constructors
152 };
153