[C++] std::stringにキャストされた場合に文字列を返すようにする

クラスのオブジェクトがstd::stringとしてキャストされた場合任意の文字列を返すようにする。
ロギングなどで複数の別々のクラスをstringとして扱って統計情報などを出力したりするのに使える。

#include <iostream>
#include <string>

class Coordinate {
private:
    int _x;
    int _y;
public:
    Coordinate(int x, int y): _y(y), _x(x) {}
    explicit operator std::string () const {
        return "x=" + std::to_string(_x) + ", y=" + std::to_string(_y);
    }
};

int main() {
    Coordinate coord(300, 400);
    std::cout << static_cast(coord) << std::endl;
}