Why is it legal to define an arbitrary type of returned overloaded operators when T or T & are expected

This is a question of what the original poster did here: Size of objects with multi-level / multiple inheritance . I hope this is true, and I could not find information on this topic.

Can anyone say why it is even legal to write such things:

#include <iostream>
using namespace std;

struct A{
    int count = 0;
    void operator ++(){count++;};
    void operator ++(int){count++;};
    void operator =(int){};
};

int main() {
    A a;
    ++a;
    a++;
    a=5;
    std::cout << a.count;
    return 0;
}

Compiles with g++ -std=c++11 -Wpedantic ./operator_overload.cppno warning. (C ++ 11 is added for the initializer in the class).

Why is it allowed by the standard to return arbitrary types when objects of the return type of the source class are expected, maybe they are referenced.

, , , . , T, , , .

, void , . - , , ?

+3
3

, ; , . , , , , .

(, << >> -, (ab) - ); , , , .

, , -> new, , , . , , .

+4

(IMO) , , , , , ++ , ++.

, .

+4

Because there is no real reason to prohibit this use. Therefore, they allow the programmer to decide whether this can be useful; the return type is not part of the resolution of overloading anyway.

The spirit-like template library uses a lot of “hacking” to implement the parser directly in EBNF-style grammar syntax and takes advantage of this freedom.

+2
source

All Articles