MoveOnly.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #ifndef MOVEONLY_H
  9. #define MOVEONLY_H
  10. #include "test_macros.h"
  11. #if TEST_STD_VER >= 11
  12. #include <cstddef>
  13. #include <functional>
  14. class MoveOnly
  15. {
  16. MoveOnly(const MoveOnly&);
  17. MoveOnly& operator=(const MoveOnly&);
  18. int data_;
  19. public:
  20. MoveOnly(int data = 1) : data_(data) {}
  21. MoveOnly(MoveOnly&& x)
  22. : data_(x.data_) {x.data_ = 0;}
  23. MoveOnly& operator=(MoveOnly&& x)
  24. {data_ = x.data_; x.data_ = 0; return *this;}
  25. int get() const {return data_;}
  26. bool operator==(const MoveOnly& x) const {return data_ == x.data_;}
  27. bool operator< (const MoveOnly& x) const {return data_ < x.data_;}
  28. MoveOnly operator+(const MoveOnly& x) const { return MoveOnly{data_ + x.data_}; }
  29. MoveOnly operator*(const MoveOnly& x) const { return MoveOnly{data_ * x.data_}; }
  30. };
  31. namespace std {
  32. template <>
  33. struct hash<MoveOnly>
  34. {
  35. typedef MoveOnly argument_type;
  36. typedef size_t result_type;
  37. std::size_t operator()(const MoveOnly& x) const {return x.get();}
  38. };
  39. }
  40. #endif // TEST_STD_VER >= 11
  41. #endif // MOVEONLY_H