DEV Community

Cover image for Creating the very long arrow operator --->
Lena
Lena

Posted on

Creating the very long arrow operator --->

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 --->

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 --->

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:

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:

#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();
}

Enter fullscreen mode Exit fullscreen mode

Compiler explorer link

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:

#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;
}
Enter fullscreen mode Exit fullscreen mode

Compiler explorer link

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

Top comments (2)

Collapse
 
pauljlucas profile image
Paul J. Lucas

I suppose the --->>> operator is next.

Collapse
 
baduit profile image
Lena

If I could make it work I would absolutely do it!
I thought about making -->> but I think it would be a bit redundant