Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the postfix increment operator take a dummy parameter?

Tags:

Have a look at these function signatures:

 class Number {  public:    Number& operator++ ();    // prefix ++    Number  operator++ (int); // postfix ++  };  

Prefix doesn't take any parameter but postfix does. Why? I thought we can recognize them with different return types.

like image 639
Naruto Avatar asked Aug 26 '10 11:08

Naruto


People also ask

Why is a dummy parameter passed to overload post increment operator?

The dummy parameter is simply there to distinguish between the postfix and prefix operators. The name ++ or -- is the same in both cases, so there has to be some way to specify which one you're defining.

How does the postfix increment operator work?

Postfix increment operator means the expression is evaluated first using the original value of the variable and then the variable is incremented(increased). Postfix decrement operator means the expression is evaluated first using the original value of the variable and then the variable is decremented(decreased).

What is a dummy parameter?

[′dəm·ē pə′ram·əd·ər] (computer science) A parameter whose value has no significance but which is included in an instruction or command to satisfy the requirements of the system.

What is the purpose of the int parameter in the operator ++( int function?

The "odd" and unused dummy int argument is used to indicate the odd postfix operators. In other words, in the postfix case, ++ comes between the first (real) operand and the second (dummy) argument and is thus postfix.


1 Answers

Prefix and postfix ++ are different operators. With the standard Foo operator symbol(Foo &) style declaration there was no obvious way to distinguish the two. Rather than come up with some new syntax like Foo symbol operator(Foo &) which would make it into a special case unlike all the other operators and likely a bit of a pain to parse, the language designers wanted some other solution.

The solution they chose was somewhat bizarre. They noted that all the other 'postfix' operators (i.e. operators that occurred after one of their operands) were actually infix operators that took two arguments. For example, plain old +, / or >. On this basis the language designers decided that having a random dummy argument would be a good way to distinguish between prefix and postfix ++.

IMHO, it's one of the stranger decisions made as C++ evolved. But there you have it.

And you can't distinguish them based on return type for two reasons.

The first is that functions in C++ cannot be overloaded on return type. You cannot have two functions that have identical names and parameter type lists but different return values.

The second is that method would not be robust or flexible enough to handle all possible implementations of prefix and postfix ++.

For example, you might want a postfix ++ that returned a reference type if the only reason you ever called it was to invoke a side-effect unrelated to the value of the variable you were applying it to. In my opinion, that would be a very bad implementation, but C++ is not about judging what kinds of stupid code you want to write, but about enabling you to write whatever code it is you think appropriate to the situation. And forcing you to use one particular style of return type for prefix ++ and postfix ++ would be contrary to that spirit.

like image 141
Omnifarious Avatar answered Jan 03 '23 09:01

Omnifarious