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 MOVEONLY_H 10 #define MOVEONLY_H 11 12 #include "test_macros.h" 13 14 #if TEST_STD_VER >= 11 15 16 #include <cstddef> 17 #include <functional> 18 19 class MoveOnly 20 { 21 MoveOnly(const MoveOnly&); 22 MoveOnly& operator=(const MoveOnly&); 23 24 int data_; 25 public: 26 constexpr MoveOnly(int data = 1) : data_(data) {} 27 TEST_CONSTEXPR_CXX14 MoveOnly(MoveOnly&& x) 28 : data_(x.data_) {x.data_ = 0;} 29 TEST_CONSTEXPR_CXX14 MoveOnly& operator=(MoveOnly&& x) 30 {data_ = x.data_; x.data_ = 0; return *this;} 31 32 constexpr int get() const {return data_;} 33 34 friend constexpr bool operator==(const MoveOnly& x, const MoveOnly& y) 35 { return x.data_ == y.data_; } 36 friend constexpr bool operator!=(const MoveOnly& x, const MoveOnly& y) 37 { return x.data_ != y.data_; } 38 friend constexpr bool operator< (const MoveOnly& x, const MoveOnly& y) 39 { return x.data_ < y.data_; } 40 friend constexpr bool operator<=(const MoveOnly& x, const MoveOnly& y) 41 { return x.data_ <= y.data_; } 42 friend constexpr bool operator> (const MoveOnly& x, const MoveOnly& y) 43 { return x.data_ > y.data_; } 44 friend constexpr bool operator>=(const MoveOnly& x, const MoveOnly& y) 45 { return x.data_ >= y.data_; } 46 47 TEST_CONSTEXPR_CXX14 MoveOnly operator+(const MoveOnly& x) const 48 { return MoveOnly{data_ + x.data_}; } 49 TEST_CONSTEXPR_CXX14 MoveOnly operator*(const MoveOnly& x) const 50 { return MoveOnly{data_ * x.data_}; } 51 }; 52 53 namespace std { 54 55 template <> 56 struct hash<MoveOnly> 57 { 58 typedef MoveOnly argument_type; 59 typedef size_t result_type; 60 constexpr size_t operator()(const MoveOnly& x) const {return x.get();} 61 }; 62 63 } 64 65 #endif // TEST_STD_VER >= 11 66 67 #endif // MOVEONLY_H 68