Home The overloaded insertion operator in C++ exceptions
Post
Cancel

The overloaded insertion operator in C++ exceptions

The << operator, when applied to streams, is called the insertion operator. (Likewise, the extraction operator is >>). If we overload it for a class (as non-member), we can output instances of that class. That’s very handy.

Note that in order for the overloaded operator<<() method to have access to private members of the class, we need to declare it as friend.

Take the very basic example of 2D coordinates:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

class Coords {
    uint16_t _x;
    uint16_t _y;
public:
    Coords(uint16_t x, uint16_t y): _x(x), _y(y)
    {

    }
    friend std::ostream& operator<<(std::ostream& out, const Coords& coords)
    {
        out << "(" << coords._x << ", " << coords._y << ")";
        return out;
    }
};

int main()
{
    Coords c(0, 1);
    std::cout << c << std::endl;
}

This produces, as expected:

1
(0, 1)

The problem with operator<<() (through my Python-tinted glasses, of course) is that it cannot unfortunately be used directly to create a custom exception description :confused:

We must use a stringstream instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <exception>
#include <sstream>

class Coords {
    // ...
    void move_left()
    {
        if (_x == 0) {
            std::stringstream ss;
            ss << "Coords: cannot move left from " << *this;
            throw std::invalid_argument(ss.str());
        }
    }
};

This produces:

1
Coords: cannot move left from (0, 1)
This post is licensed under CC BY 4.0 by the author.

Merge Mercurial branch into default

Use cases for Python environment variables

Comments powered by Disqus.