0

How do you increase the post-fix operator by more than the default of one?(i.e. applying a post-fix operator to 4 so that it increases the value to 6 instead of 5)

This question pertains to a for loop I want to create where I am to check every second number of a given string to validate. I thought incrementing the i variable by two instead of by one would achieve this.

onemic
  • 59
  • 1
  • 3
  • 10
  • 2
    Is it a `C` or `C++` question? Is the incrementation always going to be 2? Is `var += 2;` not sufficient? – AntonH Mar 14 '14 at 22:10
  • 1
    Strangely enough, C++ lets you do that, but I sure wouldn't recommend it. –  Mar 14 '14 at 22:11
  • It's for a for loop I'm creating. I need it to check every second character of a string for validation purposes. I'd want the incrementor to go up by two in order to achieve this instead of the default one. That is, unless there is a more efficient way to do this that I'm not thinking of – onemic Mar 14 '14 at 22:15
  • 2
    I would go with simply `var += 2;`. Compilers are generally smart enough to optimize small code like that. – AntonH Mar 14 '14 at 22:16
  • 2
    you should totally make a class and override the ++ operator... people would love maintaining that! – Grady Player Mar 14 '14 at 22:18
  • @unclebrad, how can you do that? I don't believe it is possible without making a wrapper class for `int` and overloading all it's functions. – Cramer Mar 14 '14 at 22:23
  • If you want to talk about bad ideal, write a wrapper class, make the int public, and only override the `++Operator` method. That way, no need to override any other methods. But it's a terrible idea. – AntonH Mar 14 '14 at 22:38

4 Answers4

3

You cannot overload the ++ operator for built in types, no. The simplest way would be to use

var += step;

and

for(i=0; i<end; i+=4) {
    ...
}

Note that using the less than instead of equality leads you to not stress about end point not lining up exactly. Of course there is always

for(i=0; i<end; ++i) {
    do_something(4*i);
}

where the number of iterations is clearer, if not the range like the previous. Whatever is clearest to you.

Cramer
  • 1,785
  • 1
  • 12
  • 20
  • `var += step` is not postfix; it returns the new value. – Kaz Mar 14 '14 at 22:31
  • 1
    @Kaz the value of the third expression isn't used, whether it's pre- or post- is irrelevant. – Alex Celeste Mar 14 '14 at 22:36
  • @Leushenko Question says "post-fix operator". A future visitor looking for an actual post-fix operator that increments by more than one might land here in search. – Kaz Mar 14 '14 at 22:40
3

You can do like:

for(int i = 0; i < n; i += x){ /* do something */ }
1

I don't know why you insist on postfix. For a simple for loop, prefix is equivalent, so do prefer it because postfix implies a copy. Anyhow, besides the apparent

for(i=0; i<end; i+=n) ...

if you're only incrementing by 2, you can also say

for(i=0; i<end; ++++i) ...

or

for(i=0; i<end; ++i, ++i) ...

but which one is more efficient depends on the optimizer (most probably they end up the same).

iavr
  • 7,547
  • 1
  • 18
  • 53
  • 2
    The second is undefined - http://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points. – Commander Coriander Salamander Mar 14 '14 at 22:45
  • @CommanderCorianderSalamander Sorry, I can't see why. One answer quotes `(§1.9/15) The value computations of the operands of an operator are sequenced before the value computation of the result of the operator.` and then gives example `++++i ; //well defined behaviour`. `++++i` is parsed as `++(++i)`. Anyhow, I'll gladly remove it if I see why it is undefined. For the moment I'm adding `++i, ++i` which (I hope) is safer. – iavr Mar 14 '14 at 23:09
  • @CommanderCorianderSalamander Also according to [this answer](http://stackoverflow.com/questions/3690141/multiple-preincrement-operations-on-a-variable-in-cc/3691469#3691469) `++++i` is not undefined. There are opposite opinions however. Maybe this is a difference between C++03 and C++11? – iavr Mar 14 '14 at 23:22
0

There is no post-fix increment operator by a step other than 1. The obj += 2 expression returns the new value, rather than the old.

If obj is a class, you could hack your += operator overload to return the old value, but it would be a bad idea.

What we can do is make a simple template function called postinc which does this for us in a type generic way:

#include <iostream>

template <typename OBJ, typename DELTA>
OBJ postinc(OBJ &obj, const DELTA &delta)
{
  OBJ oldval = obj;
  obj += delta;
  return oldval;
}

using std::cout;
using std::endl;

int main()
{
  int x = 39;
  int y = postinc(x, 3);  // <-- now we can do this
                          // x and 3 can be any objects supporting x += 3

  cout << "x = " << x << "; y = " << y << endl;
  return 0;
}

Output:

x = 42; y = 39

As you can see, y received the old value of x, and x incremented by 2.

All we did was y = postinc(x, 2). It's not fancy syntactic sugar like a post-incrementing overload of y += 2, but it does the job of sitting nicely in an expression, eliminating a clumsy breakup of the code to introduce temporary variables.

If you don't need postfix semantics, or even the result value of the expression at all, then just use var += delta.

Kaz
  • 55,781
  • 9
  • 100
  • 149