Creating the very long arrow operator --->
Tags: cpp
Published: 2023-08-01 10:58:20

---
title: Creating the very long arrow operator --->
published: true
description: 
tags: cpp
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4rjmhdh4axhrcbbhxvwe.jpg
---

## Article::Article
In my previous article, I used the long arrow `-->`, but it is possible to use an arrow even bigger! Your dreams are now a reality, let's make an entrance for the very long arrow `--->`

![Meme with the template "Cooler Daniel", Daniel is --> and cooler Daniel is ---> ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjj2kgiw0nbmwr0dkag2.jpg)

How? The same way that `-->` is a combination of `--` and `>`, the very long arrow `--->` is the combination of `--` and `->`.

![Meme with the template PPAP, combining operator -- and -> makes --->](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4rjmhdh4axhrcbbhxvwe.jpg)

## Reminders about operator ->
The operator `->` and its overload are a bit special. For the rest of the article I will assume that you know how it works. If that's not the case, I recommend you to read:
* [this article](https://dev.to/baduit/surcharger-loperateur-en-c-lde) if you read French
* otherwise [the first response of this Stack Overflow thread](https://stackoverflow.com/questions/8777845/overloading-member-access-operators)

## Implementation
### Basic
The first step is to define two classes: __Base__ and __Extension__. Then overload the operator `--` of __Base__ to create and return an object of type __Extension__. After that, overload the operator `->` of __Extension__ to return a pointer to itself. The last step is to add a member function to __Extension__. It is now possible to use a very long arrow `--->`. Here's the code:
```C++
#include <utility>
#include <iostream>

struct Base {};

struct Extension
{
    const Extension* operator->() const
    {
        return this;
    }

    void print() const
    {
        std::cout << "long arrow" << std::endl;
    }
};

Extension operator--(const Base&, int)
{
    return Extension{};
}

int main()
{
    Base base;
    base--->print();
}

```
[Compiler explorer link](https://godbolt.org/z/hxMvz4nPx)

## Possible usage
The previous example is totally useless, let's see a possible usage of what you can do with it: extend a class without touching it.

Let's imagine I want to add a simple method to __std::string__, like something to transform it from upper case to lower case. It is possible to do this:
```C++
#include <cctype>
#include <string>
#include <iostream>
#include <algorithm>

struct StringExtension
{
    StringExtension* operator->()
    {
        return this;
    }

    void to_lower()
    {
        auto lower = [](unsigned char c) { return std::tolower(c); };
        std::transform(str.begin(), str.end(), str.begin(), lower);
    }

    std::string& str;
};

StringExtension operator--(std::string& str, int)
{
    return StringExtension{ str };
}

int main()
{
    std::string str = "HELLO";
    str--->to_lower();
    std::cout << str << std::endl;
}
```
[Compiler explorer link](https://godbolt.org/z/PdThsY399)

The logic is simple: create a class with all the member function we want to add with a reference to the class you want to extend. Overload the operator `->` of this new class to return a pointer to itself and then overload the operator `--` of the class you want to extend returning an object of the extension class. That's it.


## Article::~Article
That's add another string to your bow and you can be special snowflake that uses very loooooong arrows.

I hope you enjoyed this article as much as me when I wrote it and that you will find something funny, or even useful, to do with what you learned.

## Sources
* [https://en.cppreference.com/w/cpp/language/operators](https://en.cppreference.com/w/cpp/language/operators)