Search code examples
c++standardsunary-operatorbinary-operatorspostfix-operator

Shouldn't the Postfix Operators be Considered Binary Operators


The postfix operators take an int parameter. There is already a question as to why, and it seems like the answer is: "Because Bjarne Stroustrup said so"

I'm uncomfortable with that answer. If Bjarne Stroustrup needed something to tip the compiler off to behave differently, why couldn't he just key off whether the operator returned a reference? It leaves me questioning:

  1. Why can't I do: foo++ 13;
  2. Why isn't the int parameter defaulted to 1
  3. Why is this considered a unary operator at all, it takes an argument

Solution

  • If Bjarne Stroustrup needed something to tip the compiler off to behave differently, why couldn't he just key off whether the operator returned a reference?

    Because you cannot overload a function based on its return type. It is the parameters, the const qualification and reference qualification that the function can be overloaded for

    Why can't I do: foo++ 13;

    Because the (int) parameter is just there for overload resolution. You do not take it in or use the parameter.

    Why isn't the int parameter defaulted to 1

    Again it is not used. It is just there to tell the compiler if it is the prefix or postfix version.

    Why is this considered a unary operator at all, it takes an argument

    It actually doesn't take an argument. The parameter is only there to make them different. It only affects and works on one operand so it is a unary operator.