1

Is there a case where this would be valid C++ code?

int main()
{
    int k = 0;
    ++f(k);
}
PetrosB
  • 4,134
  • 5
  • 22
  • 21

7 Answers7

4

Yes, if f returns a reference.

int main()
{
    int k = 0;
    ++f(k);
}

int & f(int & k) {
    return k;
}

Edit:

As pointed out in the comments, there is another possibility: you can call ++f(k), if f returns a class type and it has the ++operator(void) overloaded as a member function.

jupp0r
  • 4,502
  • 1
  • 27
  • 34
  • Also, if `f` returns an object of class type, with an overloaded `operator++`. The overload is a member function, and you *can* call a member function on an _rvalue_. – Mike Seymour Feb 25 '12 at 11:22
  • @MikeSeymour: thanks for pointing this out, I did try this with int, and it did not work, but did not try with a class type. My fault. – jupp0r Feb 25 '12 at 12:06
  • Indeed, it won't work for non-class types; the built-in operator can only be applied to an _lvalue_, while member functions can also be called on an _rvalue_. – Mike Seymour Feb 25 '12 at 18:11
  • That's what I like about stackoverflow, even if you just answer questions you still learn a lot. – jupp0r Feb 25 '12 at 18:15
3

Well, for one, it would work if you preceded that code with:

#define f

but that's not applying the increment operator to a function, as you seem to desire in your title. If you try to apply it to a function as below:

int f (int x) { return x + 1; }

you will get the error:

error: lvalue required as increment operand

because the returned value is not an lvalue (obviously). You can return a reference to a variable which can then be incremented thus:

#include <iostream>
int xyzzy = 42;
int & f(int x) { return xyzzy; }
int main() {
    int k = 0;
    ++f(k);
    std::cout << xyzzy << '\n';
    return 0;
}

This works because the returned reference references the xyzzy variable; it outputs:

43
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Any case where this would be useful? – PetrosB Feb 25 '12 at 10:18
  • @Petros, assuming you mean the `#define` bit, probably not. The reference bit I could see being potentially useful, but I'm sure there's better ways to do that as well. – paxdiablo Feb 25 '12 at 10:20
3

The function f will be run before the ++ pre-operator, This is due to precedence of operators. You are not incrementing the function but the value it returns. So this code would only be valid if the value returned by f(k) can be incremented.

In short, yes, this code is valid in a lot of cases (when the object f returns is not const and can be incremented).

qdii
  • 12,505
  • 10
  • 59
  • 116
1

f() could return a reference that you want to increase after returning from the function. for example:

int someGlobal;
int& f(int k) {
 return someGlobal;
}

Though it is wrong in many ways [designically speaking]

amit
  • 175,853
  • 27
  • 231
  • 333
1

In the obvious case that f has the signature int f(int) or some equivalent, no: you cannot use ++ on a temporary. It would not even compile.

If it's made to compile by some other trick (such as f returning a reference) then there's still the matter that code like this is an example of what to avoid.

So it all depends on what you mean by valid: it can be valid as the compiler is concerned, but IMO it's not valid in the sense that I could read this code and not have some choice things to say about the author.

Jon
  • 428,835
  • 81
  • 738
  • 806
1

Sure: Suppose you want one more than a square number:

int & square(int & n)
{
    n = n * n;
    return n;
}

Now you can say,

int k;
++f(k);

and it has the same effect as int k = 0 * 0 + 1;. The example is not so exciting for 0, but makes sense in general. It's questionable whether it is good style to have a function both mutate the argument and return a reference to it, but the pre-increment has the same semantics, and you can write ++square(++square(k)) if you like.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

When f returns an int&, for example.

Dan
  • 1,927
  • 2
  • 24
  • 35
  • @delnan: An `int&` *is* an lvalue. – celtschk Feb 25 '12 at 10:27
  • @celtschk I'm pretty certain the answer used to say `int`, and it seems at least one person agreed (my comment has +1 and the answer used to have a downvote). Anyway, removed it now. –  Feb 25 '12 at 10:40
  • @delnan: I thought of that possibility, but SO didn't display an edit of the answer (and still doesn't). Can an edit disappear? – celtschk Feb 25 '12 at 10:48
  • @celtschk Edits within the first few minutes are never shown, or at least have never been shown to me. –  Feb 25 '12 at 10:51
  • @delnan Yes, the answer was initially about int, not int&. I saw it too :). But anyways, this answer is now correct. – PetrosB Feb 25 '12 at 11:11
  • Thanks for clearing that up :) –  Feb 25 '12 at 11:19