anechka_ne_plach

Let me help you

Oct 9th, 2021 (edited)
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.56 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <type_traits>
  4. #include <utility>
  5. #include <cstddef>
  6.  
  7. template <typename T, std::size_t I, bool = std::is_empty_v<T> && !std::is_final_v<T>>
  8. struct CompressedPairElement {
  9. public:
  10.     CompressedPairElement() {
  11.     }
  12.     template <typename Tp>
  13.     CompressedPairElement(Tp&& other) : value_(std::forward<Tp>(other)) {
  14.     }
  15.     const T& Get() const {
  16.         return value_;
  17.     }
  18.     T& Get() {
  19.         return value_;
  20.     }
  21.  
  22. private:
  23.     T value_{};
  24. };
  25.  
  26. template <typename T, std::size_t I>
  27. struct CompressedPairElement<T, I, true> : public T {
  28. public:
  29.     CompressedPairElement() {
  30.     }
  31.     template <typename Tp>
  32.     CompressedPairElement(Tp&& other) {
  33.     }
  34.     const T& Get() const {
  35.         return *this;
  36.     }
  37.     T& Get() {
  38.         return *this;
  39.     }
  40. };
  41.  
  42. template <typename F, typename S>
  43. class CompressedPair : protected CompressedPairElement<F, 0>,
  44.                        protected CompressedPairElement<S, 1> {
  45.     using First = CompressedPairElement<F, 0>;
  46.     using Second = CompressedPairElement<S, 1>;
  47.  
  48. public:
  49.     CompressedPair() {
  50.     }
  51.     template <typename U, typename V>
  52.     CompressedPair(U&& first, V&& second)
  53.         : First(std::forward<decltype(first)>(first)),
  54.           Second(std::forward<decltype(second)>(second)) {
  55.     }
  56.     F& GetFirst() {
  57.         return First::Get();
  58.     }
  59.     S& GetSecond() {
  60.         return Second::Get();
  61.     }
  62.     const F& GetFirst() const {
  63.         return First::Get();
  64.     }
  65.     const S& GetSecond() const {
  66.         return Second::Get();
  67.     }
  68. };
  69.  
Add Comment
Please, Sign In to add comment