Can I increase int by more than one using i ++ syntax?

int fkt (int & i) {return i ++; }

int main() { int i = 5; printf("%d ", fkt(i)); printf("%d ", fkt(i)); printf("%d ", fkt(i)); } 

prints '5 6 7'. Say I want to print "5 7 9" as follows: is it possible to do the same without a temporary variable in fkt ()? (A temporary variable will slightly decrease efficiency, right?) Ie, something like

 return i+=2 

or

 return i, i+=2; 

which first increments i and then returns it, which is not what I need.

thanks

EDIT: The main reason I do this is in a function, not the outside, because fkt will be a function pointer. The original function will do something else with i. I just feel like using {int temp = i; i + = 2; return temp;} does not look as good as {return i ++;}.

I don't need printf, this is just to illustrate the use of the result.

EDIT2: Wow, there seems to be more chat than a traditional board :) Thanks for all the answers. My fkt is actually that. Depending on some condition, I define get_it as get_it_1, get_it_2, and get_it_4:

 unsigned int (*get_it)(char*&); unsigned int get_it_1(char* &s) {return *((unsigned char*) s++);} unsigned int get_it_2(char* &s) {unsigned int tmp = *((unsigned short int*) s); s += 2; return tmp;} unsigned int get_it_4(char* &s) {unsigned int tmp = *((unsigned int*) s); s += 4; return tmp;} 

For get_it_1 it is so simple ... I will try to give more information in the future ...

+4
source share
9 answers

"A temporary variable will slightly reduce efficiency, right?"

Wrong.

Did you measure it? Keep in mind that ++ is magically effective on PDP-11. On most other processors, this is exactly the same as +=1 . Please measure these two to see what the actual differences really are.

+28
source

(A temporary variable will slightly reduce efficiency, right?)

If this is the main reason you are asking, then you worry too soon. Do not check your compiler until you have a performance issue.

If you want fkt to be able to add different amounts to i, you need to pass a parameter. There is no material reason to prefer ++ over += .

+18
source

You can zoom in twice and then subtract, something like:

 return (i += 2) - 2 

Just answering this question, I don't think you should be scared of using a temporary variable.

+9
source

If you really need this speed, check out the build code that the compiler produces. If you do:

 int t = i; i+=2; return t; 

then the compiler can optimize it by inserting a function into something like:

 printf("%d ", i); i+=2; 

Which is as good as it is going.

EDIT: Now you say you jump to this function with a function pointer? Calling a function with a pointer is rather slow compared to using a temporary variable. I'm not sure, but I think that I remember Intel documents talking about it somewhere around 20-30 CPU cycles on Core 2 and i7, that is, if your function code is in the cache. Since the compiler cannot inline your function when it is called by a pointer, ++ will also create a temporary variable.

+4
source

A temporary variable will slightly decrease efficiency, right?

Always consider "premature optimization is the root of all evil." Spend time in other areas, then try to optimize it. + = and ++ are likely to do the same. Your computer will control;)

+3
source

Well, you could do

 return i+=2, i-2; 

However, just follow this simple maxim:

Enter code that is easy to understand.

Code that is easy for humans to understand is usually easy for the compiler to understand, and therefore the compiler can optimize it.

Writing crazy things to optimize your code often just confuses the compiler and makes it harder for the compiler to optimize your code.

I would recommend

 int fkt(int &i) { int orig = i; i += 2; return orig; } 
+3
source

A “temporary variable” would be very unlikely to decrease performance, since I ++ should contain the old state inside the country (i.e. it implicitly uses what you call a temporary variable). However, the instruction that you want to increase by two may be slower than the command used for ++.

You may try:

 int fkt(int &i) { ++ii; return i++; } 

You can compare this with:

 int fkt(int &i) { int t = i; i += 2; return t; } 

However, I do not think that you are prematurely concerned with such performance considerations.

+2
source

I think what you really want is a functor:

 class Fkt { int num; public: Fkt(int i) :num(i-2) {} operator()() { num+=2; return num; } } int main() { Fkt fkt(5); printf("%d ", fkt()); printf("%d ", fkt()); printf("%d ", fkt()); } 
+1
source

What about

 int a = 0; ++++a; 

Increments +2 without time variables.

EDIT: Oh, sorry: didn't notice that you want to reach 5,7,9. This requires temporary variables, so the prefix notation cannot be used.

-1
source

Source: https://habr.com/ru/post/1315664/


All Articles