Andrew
Andrew

Reputation: 165

C++ Constant return type of suffix increment operator

In C++, wherever I see in web an example of suffix increment operator declaration, it is always declared as

T& operator++(int);

and I believe this is the correct syntax of a suffix increment, isn't it?

The issue is that, whenever I declare suffix increment, I declare return type with const keyword, so that it becomes lvalue-like.

Please see the example code:

class AClass
{
    int foo;

public:
    AClass(void) : foo(0) {};

    // Suffix increment operator
    // Consider adding const to return type
    /* const */ AClass operator++(int)
    {
        AClass cp(*this);
        foo++;
        return cp;
    };

    // Prefix increment operator
    AClass& operator++()
    {
        foo++;
        return *this;
    };
};

int main(int argc, const char* args[])
{
    /* This code would fail to compile.
    int bar = 5;
    (bar++)++;
     */

    // Similarily, I would expect this to fail
    //   but it will succeed unless I use const return type.
    AClass a;
    (a++)++;
}

I have never had problems about such a const-declared operator and I know it already saved our code from a bug made by a clumsy co-worker. So, my questions are:

  1. Are there any cons for such a practice? Is it a good practice indeed?
  2. What is the really correct declaration of suffix operator (I mean standards)?
  3. If this is not how the standard specifies but is already a good practice, shouldn't it become a standard?

Thanks a lot for your answers!

Upvotes: 0

Views: 1048

Answers (2)

Alexandre C.
Alexandre C.

Reputation: 56976

Suffix increment returns a temporary, not a reference (this means your first signature is wrong):

T& operator++() // prefix
{
    this->increment();
    return *this;
}

T operator++(int) // suffix
{
    // Almost always, you'll have this code:
    T tmp(*this); ++(*this); return tmp;
}

Some people like to const-qualify the return value of the suffix operator to avoid writing stupid things like

(a++).modify_me();

which doesn't modify a (it applies modify_me to a temporary object). Contrast with

(++a).modify_me();

which increments a and then modifies it.

Personally, I don't think it is necessary (since you may be interested in the side effects of modify_me). Moreover, in C++11, you may want to bind said temporary to a (non const) rvalue reference. Const qualifying the return type of suffix operators disables this possibility.

Upvotes: 6

Benjamin Lindley
Benjamin Lindley

Reputation: 103741

and I believe this is the correct syntax of a suffix increment, isn't it?

If by "correct", you mean "common practice", then no. If you're trying to create behavior analagous to the postfix operator on integers, then it should return by value.

const T operator++(int);

This is because it makes a copy, then increments, then returns the copy. Since the copy is local, you definitely don't want to return it by reference.

The const you can take or leave, but the return by value rather than reference is essential.

Upvotes: 1

Related Questions