Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <type_traits>
- #include <utility>
- #include <cstddef>
- template <typename T, std::size_t I, bool = std::is_empty_v<T> && !std::is_final_v<T>>
- struct CompressedPairElement {
- public:
- CompressedPairElement() {
- }
- template <typename Tp>
- CompressedPairElement(Tp&& other) : value_(std::forward<Tp>(other)) {
- }
- const T& Get() const {
- return value_;
- }
- T& Get() {
- return value_;
- }
- private:
- T value_{};
- };
- template <typename T, std::size_t I>
- struct CompressedPairElement<T, I, true> : public T {
- public:
- CompressedPairElement() {
- }
- template <typename Tp>
- CompressedPairElement(Tp&& other) {
- }
- const T& Get() const {
- return *this;
- }
- T& Get() {
- return *this;
- }
- };
- template <typename F, typename S>
- class CompressedPair : protected CompressedPairElement<F, 0>,
- protected CompressedPairElement<S, 1> {
- using First = CompressedPairElement<F, 0>;
- using Second = CompressedPairElement<S, 1>;
- public:
- CompressedPair() {
- }
- template <typename U, typename V>
- CompressedPair(U&& first, V&& second)
- : First(std::forward<decltype(first)>(first)),
- Second(std::forward<decltype(second)>(second)) {
- }
- F& GetFirst() {
- return First::Get();
- }
- S& GetSecond() {
- return Second::Get();
- }
- const F& GetFirst() const {
- return First::Get();
- }
- const S& GetSecond() const {
- return Second::Get();
- }
- };
Add Comment
Please, Sign In to add comment