xref: /llvm-project/libcxx/test/support/operator_hijacker.h (revision 09e3a360581dc36d0820d3fb6da9bd7cfed87b5d)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef SUPPORT_OPERATOR_HIJACKER_H
10 #define SUPPORT_OPERATOR_HIJACKER_H
11 
12 #include <cstddef>
13 #include <functional>
14 #include <memory>
15 #include <string>
16 #include <type_traits>
17 
18 #include "test_macros.h"
19 
20 /// Helper struct to test ADL-hijacking in containers.
21 ///
22 /// The class has some additional operations to be usable in all containers.
23 struct operator_hijacker {
24   TEST_CONSTEXPR bool operator<(const operator_hijacker&) const { return true; }
25   TEST_CONSTEXPR bool operator==(const operator_hijacker&) const { return true; }
26 
27   template <typename T>
28   friend void operator&(T&&) = delete;
29   template <class T, class U>
30   friend void operator,(T&&, U&&) = delete;
31   template <class T, class U>
32   friend void operator&&(T&&, U&&) = delete;
33   template <class T, class U>
34   friend void operator||(T&&, U&&) = delete;
35 };
36 
37 static_assert(std::is_trivially_copyable<operator_hijacker>::value &&     //
38                   std::is_copy_constructible<operator_hijacker>::value && //
39                   std::is_move_constructible<operator_hijacker>::value && //
40                   std::is_copy_assignable<operator_hijacker>::value &&    //
41                   std::is_move_assignable<operator_hijacker>::value,      //
42               "does not satisfy the requirements for atomic<operator_hijacker>");
43 
44 template <>
45 struct std::hash<operator_hijacker> {
46   std::size_t operator()(const operator_hijacker&) const { return 0; }
47 };
48 
49 template <class T>
50 struct operator_hijacker_allocator : std::allocator<T>, operator_hijacker {
51 #if TEST_STD_VER <= 17
52   struct rebind {
53     typedef operator_hijacker_allocator<T> other;
54   };
55 #endif
56 };
57 
58 template <class CharT>
59 struct operator_hijacker_char_traits : std::char_traits<CharT>, operator_hijacker {};
60 
61 #endif // SUPPORT_OPERATOR_HIJACKER_H
62