Advertisement
kalabukdima

CRTP for code deduplication

Nov 17th, 2020
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5.  
  6. template <typename Derived>
  7. struct Iterable {
  8. public:
  9.     Derived& Myself() {
  10.         return static_cast<Derived&>(*this);
  11.     }
  12.     const Derived& Myself() const {
  13.         return static_cast<const Derived&>(*this);
  14.     }
  15.  
  16.     size_t Size() const {
  17.         return std::distance(Myself().Begin(), Myself().End());
  18.     }
  19.  
  20.     auto& Front() {
  21.         return *Myself().Begin();
  22.     }
  23.     auto& Back() {
  24.         return *std::prev(Myself().End());
  25.     }
  26.  
  27.     auto& operator[](size_t i) {
  28.         return Myself().Begin()[i];
  29.     }
  30.     const auto& operator[](size_t i) const {
  31.         return Myself().Begin()[i];
  32.     }
  33. };
  34.  
  35. template <typename Derived>
  36. struct Comparable {
  37.     Derived& Myself() {
  38.         return static_cast<Derived&>(*this);
  39.     }
  40.     const Derived& Myself() const {
  41.         return static_cast<const Derived&>(*this);
  42.     }
  43.  
  44.     bool operator>(const Derived& rhs) const {
  45.         return rhs < Myself();
  46.     }
  47.  
  48.     bool operator>=(const Derived& rhs) const {
  49.         return !(Myself() < rhs);
  50.     }
  51.  
  52.     bool operator<=(const Derived& rhs) const {
  53.         return !(rhs < Myself());
  54.     }
  55.  
  56.     bool operator==(const Derived& rhs) const {
  57.         return !(Myself() < rhs) && !(rhs < Myself());
  58.     }
  59. };
  60.  
  61.  
  62. struct String : public Comparable<String>, public Iterable<String> {
  63.     std::string value;  // oversimplified for demonstrative purposes
  64.  
  65.     String(std::string s) : value(std::move(s)) {}
  66.  
  67.     bool operator<(const String& rhs) const {
  68.         std::cout << "Called <\n";
  69.         return value < rhs.value;
  70.     }
  71.  
  72.     // If it doesn't exist, base class implementation is called
  73.     // bool operator==(const String& rhs) const {
  74.     //     std::cout << "Called ==\n";
  75.     //     return value == rhs.value;
  76.     // }
  77.  
  78.     auto Begin() {
  79.         return value.begin();
  80.     }
  81.     auto Begin() const {
  82.         return value.begin();
  83.     }
  84.     auto End() {
  85.         return value.end();
  86.     }
  87.     auto End() const {
  88.         return value.end();
  89.     }
  90. };
  91.  
  92.  
  93. void Compilable() {
  94.     String a{"abc"};
  95.     const String b{"axyz"};
  96.     a < b;
  97.     a > b;
  98.     a == b;
  99.     a <= b;
  100.     a >= b;
  101.     a[1] = 'a';
  102.     // b[1] = 'a';
  103.     b.Size();
  104. }
  105.  
  106. int main() {
  107.     String a{"abc"};
  108.     const String b{"axyz"};
  109.     a == b;
  110.     std::cout << b[1] << "\n";
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement