오버로드 할 수 있는 연산자 목록

오버로딩은 다양한 연산자를 지원한다.

전위/후위 연산자

    Foo& operator++()
    {
        cout << "pre" << endl;
        return *this;
    }
		// 뒤에 int는 컴파일러가 후위 증가 연산자를 전위 증가 연산자와 구별하기 위해 사용된다.
    Foo operator++(int)
    {
        cout << "post" << endl;
        return *this;
    }

std::exchange

std::rel_ops

#include <iostream>
#include <utility>
 
struct Foo
{
    int n;
};
 
bool operator==(const Foo& lhs, const Foo& rhs)
{
    return lhs.n == rhs.n;
}
 
bool operator<(const Foo& lhs, const Foo& rhs)
{
    return lhs.n < rhs.n;
}
 
int main()
{
    Foo f1 = {1};
    Foo f2 = {2};
    using namespace std::rel_ops;
 
    std::cout << std::boolalpha
              << "{1} != {2} : " << (f1 != f2) << '\\n'
              << "{1} >  {2} : " << (f1 >  f2) << '\\n'
              << "{1} <= {2} : " << (f1 <= f2) << '\\n'
              << "{1} >= {2} : " << (f1 >= f2) << '\\n';
}

타입 변환식

struct Foo
{
    Foo(double a)
        : z(a)
    {
    }

    explicit operator double() const
    {
        cout << "double" << endl;
        return z;
    }

    Foo& operator+(const Foo&)
    {
        cout << "operator+" << endl;
        return *this;
    }

    double z;
};

int main()
{
    Foo foo { 6.6 };
    double a { foo + 3.3 };

    return 0;
}

부울 표현식

Placement new operator

사용자 정의 리터럴 연산자 오버로딩