88 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef REP_H10#define REP_H11 12#include "test_macros.h"13#include <type_traits>14 15class Rep16{17 int data_;18public: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#4113030 31struct NotARep {};32 33#if TEST_STD_VER >= 1134// Several duration operators take a Rep parameter. Before LWG3050 this35// 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'd37// away for this type. LWG3050 fixes the constraint to use a const38// object.39struct RepConstConvertibleLWG3050 {40 operator long() = delete;41 operator long() const { return 2; }42};43 44template <>45struct std::common_type<RepConstConvertibleLWG3050, int> {46 using type = long;47};48template <>49struct std::common_type<int, RepConstConvertibleLWG3050> {50 using type = long;51};52 53#endif // TEST_STD_VER >= 1154 55// std::chrono:::duration has only '*', '/' and '%' taking a "Rep" parameter56 57// Multiplication is commutative, division is not.58template <class Rep, class Period>59std::chrono::duration<Rep, Period>60operator*(std::chrono::duration<Rep, Period> d, NotARep) { return d; }61 62template <class Rep, class Period>63std::chrono::duration<Rep, Period>64operator*(NotARep, std::chrono::duration<Rep, Period> d) { return d; }65 66template <class Rep, class Period>67std::chrono::duration<Rep, Period>68operator/(std::chrono::duration<Rep, Period> d, NotARep) { return d; }69 70template <class Rep, class Period>71std::chrono::duration<Rep, Period>72operator%(std::chrono::duration<Rep, Period> d, NotARep) { return d; }73 74// op= is not commutative.75template <class Rep, class Period>76std::chrono::duration<Rep, Period>&77operator*=(std::chrono::duration<Rep, Period>& d, NotARep) { return d; }78 79template <class Rep, class Period>80std::chrono::duration<Rep, Period>&81operator/=(std::chrono::duration<Rep, Period>& d, NotARep) { return d; }82 83template <class Rep, class Period>84std::chrono::duration<Rep, Period>&85operator%=(std::chrono::duration<Rep, Period>& d, NotARep) { return d; }86 87#endif // REP_H88