0

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
Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288

2 Answers2

6

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.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 1
    Short version: it's a hack. – Pete Becker May 05 '16 at 12:03
  • You're playing both sides of the coin: 1) "Because you cannot overload a function based on its' return type" You're saying the method must follow the traditional rules 2) "Because the (`int`) parameter is just there for overload resolution. You do not take it in or use the parameter." You're saying the method doesn't have to follow traditional rules. I'm saying if it gets to break the rules anyway at least it should make sense. – Jonathan Mee May 05 '16 at 12:06
  • @JonathanMee There isn't way in the language to tell the compiler if `operator++` should be the prefix or postfix operator. They decide to hack it in using overload resolution. Instead of making a special rule for the treatment of return value they decide that the parameter would be the marker for the compiler. It is about as natural as you can get with the way C++ works. In fact we use discarded parameters in other places as well to disambiguate overloads for the compiler. – NathanOliver May 05 '16 at 12:09
  • Gah! Other places too! This is just like an itch that I can't scratch. Declaring a function that takes an `int` argument... but doesn't accept anything, that just violates the rules. I guess I just have to accept it and move on >:-[ – Jonathan Mee May 05 '16 at 12:21
  • 1
    @JonathanMee Yeah. this is just one of those cases where instead of making new keywords or special rules for return types they just decide to use the built in mechanics of overload resolution and discard the fact there is a unused parameter. – NathanOliver May 05 '16 at 12:27
2

To answer the question in the title: in foo++, ++ is clearly a unary operator. The fact that its implementation could look like a binary operator if you squint at it just right doesn't change how it's used, and that's what makes it unary.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • So [I asked NathanOliver](http://stackoverflow.com/questions/37049991/shouldnt-the-postfix-operators-be-considered-binary-operators#comment61647719_37050082) this too, but if the way it's used doesn't follow the rules, why does the way it's detected have to? Why couldn't we key off return type for an operator that's such an anomaly anyway? – Jonathan Mee May 05 '16 at 12:09
  • It's not an anomaly at all. `int i = 0; i++;` `my_type foo; foo++;`. In both cases, `++` is a unary operator. That's consistent and useful; "the way it's used" is the same in both cases. – Pete Becker May 05 '16 at 12:11
  • The anomaly is that `my_type& operator+=(int);` is a binary operator and `my_type operator++(int);` is a unary operator. – Jonathan Mee May 05 '16 at 12:16
  • 1
    You're looking in the wrong end of the telescope. Whether an operator is unary or binary is determined by how it's used **in your source code**, not by **how it's implemented**. In `my_type foo; foo++;`, `++` is a unary operator **because** it operates on a **single value**. In `my_type foo; foo += 3;`, `+=` is a binary operator because it operates on **two values**. – Pete Becker May 05 '16 at 13:19