xref: /llvm-project/libcxx/test/std/time/rep.h (revision 5dfdac74cadd9483a66eb17e51dc632b554cccb1)
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 REP_H
10 #define REP_H
11 
12 #include "test_macros.h"
13 #include <type_traits>
14 
15 class Rep
16 {
17     int data_;
18 public:
19     TEST_CONSTEXPR Rep() : data_(-1) {}
20     explicit TEST_CONSTEXPR Rep(int i) : data_(i) {}
21 
22     bool TEST_CONSTEXPR operator==(int i) const {return data_ == i;}
23     bool TEST_CONSTEXPR operator==(const Rep& r) const {return data_ == r.data_;}
24 
25     Rep& operator*=(Rep x) {data_ *= x.data_; return *this;}
26     Rep& operator/=(Rep x) {data_ /= x.data_; return *this;}
27 };
28 
29 // This is PR#41130
30 
31 struct NotARep {};
32 
33 #if TEST_STD_VER >= 11
34 // Several duration operators take a Rep parameter. Before LWG3050 this
35 // parameter was constrained to be convertible from a non-const object,
36 // but the code always uses a const object. So the function was SFINAE'd
37 // away for this type. LWG3050 fixes the constraint to use a const
38 // object.
39 struct RepConstConvertibleLWG3050 {
40   operator long() = delete;
41   operator long() const { return 2; }
42 };
43 
44 template <>
45 struct std::common_type<RepConstConvertibleLWG3050, int> {
46   using type = long;
47 };
48 template <>
49 struct std::common_type<int, RepConstConvertibleLWG3050> {
50   using type = long;
51 };
52 
53 #endif // TEST_STD_VER >= 11
54 
55 // std::chrono:::duration has only '*', '/' and '%' taking a "Rep" parameter
56 
57 // Multiplication is commutative, division is not.
58 template <class Rep, class Period>
59 std::chrono::duration<Rep, Period>
60 operator*(std::chrono::duration<Rep, Period> d, NotARep) { return d; }
61 
62 template <class Rep, class Period>
63 std::chrono::duration<Rep, Period>
64 operator*(NotARep, std::chrono::duration<Rep, Period> d) { return d; }
65 
66 template <class Rep, class Period>
67 std::chrono::duration<Rep, Period>
68 operator/(std::chrono::duration<Rep, Period> d, NotARep) { return d; }
69 
70 template <class Rep, class Period>
71 std::chrono::duration<Rep, Period>
72 operator%(std::chrono::duration<Rep, Period> d, NotARep) { return d; }
73 
74 // op= is not commutative.
75 template <class Rep, class Period>
76 std::chrono::duration<Rep, Period>&
77 operator*=(std::chrono::duration<Rep, Period>& d, NotARep) { return d; }
78 
79 template <class Rep, class Period>
80 std::chrono::duration<Rep, Period>&
81 operator/=(std::chrono::duration<Rep, Period>& d, NotARep) { return d; }
82 
83 template <class Rep, class Period>
84 std::chrono::duration<Rep, Period>&
85 operator%=(std::chrono::duration<Rep, Period>& d, NotARep) { return d; }
86 
87 #endif // REP_H
88