Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <string>
- #include <iostream>
- template <class Derived>
- struct Comparable {
- const Derived& Myself() const {
- return static_cast<const Derived&>(*this);
- }
- inline bool operator>(const Derived& rhs) {
- return rhs < Myself();
- }
- inline bool operator>=(const Derived& rhs) {
- return !(Myself() < rhs);
- }
- inline bool operator<=(const Derived& rhs) {
- return !(rhs < Myself());
- }
- inline bool operator==(const Derived& rhs) {
- return !(Myself() < rhs) && !(rhs < Myself());
- }
- inline bool operator!=(const Derived& rhs) {
- return !(Myself() == rhs);
- }
- };
- template <class Derived>
- struct Iterable {
- const Derived& Myself() const {
- return static_cast<const Derived&>(*this);
- }
- Derived& Myself() {
- return static_cast<Derived&>(*this);
- }
- size_t Size() const {
- return Myself().End() - Myself().Begin();
- }
- auto& operator[](size_t index) {
- return *(Myself().Begin() + index);
- }
- const auto& operator[](size_t index) const {
- return *(Myself().Begin() + index);
- }
- auto& Front() {
- return *(Myself().Begin());
- }
- const auto& Front() const {
- return *(Myself().Begin());
- }
- auto& Back() {
- return *(Myself().End() - 1);
- }
- const auto& Back() const {
- return *(Myself().End() - 1);
- }
- };
- struct String : public Comparable<String>, public Iterable<String> {
- bool operator<(const String& rhs) const {
- std::cout << "Called operator<\n";
- return value < rhs.value;
- }
- bool operator==(const String& rhs) const {
- std::cout << "Called operator==\n";
- return value == rhs.value;
- }
- auto Begin() {
- return value.begin();
- }
- auto Begin() const {
- return value.begin();
- }
- auto End() {
- return value.end();
- }
- auto End() const {
- return value.end();
- }
- std::string value;
- };
- int main() {
- String a;
- a.value.push_back('A');
- a.value.push_back('B');
- a.value.push_back('C');
- std::cout << a.Size() << "\n";
- a[0] = 'D';
- std::cout << a.value << "\n";
- std::cout << a[0] << "\n";
- std::cout << a.Front() << " " << a.Back() << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement