Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

should I take arguments to inline functions by reference or value?

Tags:

c++

inline

Is one of these faster?

inline int ProcessByValue(int i) {     // process i somehow }  inline int ProcessByReference(const int& i) {     // process i somehow } 

I know that integral types should be passed by value. However, I am concerned that the compiler might inline ProcessByValue to contain a copy. Is there a rule for this?

like image 256
rlbond Avatar asked Apr 06 '09 16:04

rlbond


People also ask

Can inline function have arguments?

As with normal functions, there's no defined order for argument evaluation in an inline function. In fact, it could be different from the argument evaluation order when passed using the normal function-call protocol.

What should we not use while making a function inline?

When we should avoid the use of inline? We should not use functions that are I/O bound as inline functions. When large code is used in some function, then we should avoid the inline. When recursion is used, inline function may not work properly.

What is correct for inline functions?

Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time.

What is the main advantage of passing arguments by reference?

The advantage of passing an argument ByRef is that the procedure can return a value to the calling code through that argument. The advantage of passing an argument ByVal is that it protects a variable from being changed by the procedure.


2 Answers

It doesn't make a difference. In both case, the code will be inlined to the same. Needlessly copying the int (in pass-by-value) will be eliminated by the compiler, and needlessly creating a reference to the int, and following that layer of indirection when accessing the int, will also be eliminated.

Your question seems to be based on some false assumptions:

  • That the inline keyword will actually get your function inlined. (It might, but that's certainly not guaranteed)
  • That the choice of reference vs value depends on the function being inline. (The exact same performance considerations would apply to a non-inlined function)
  • That it makes a difference, and that you can outsmart the compiler with trivial changes like this (The compiler will apply the same optimizations in either case)
  • And that the optimization would actually make a measurable difference in performance. (even if it didn't, the difference would be so small as to be negligible.)

I know that integral types should be passed by value. However, I am concerned that the compiler might inline ProcessByValue to contain a copy. Is there a rule for this?

Yes, it will create a copy. Just like passing by reference would create a reference. And then, at least for simple types like ints, the compiler would eliminate both again. Inlining a function is not allowed to change the behavior of a function. If you create the function to take a value argument, it will behave as if it was given a value argument, whether or not it's inlined. If you define the function to take a reference, it will behave as if passed a reference, whether or not it's inlined. So do what leads to correct behavior.

like image 157
jalf Avatar answered Sep 18 '22 15:09

jalf


The parameter should be typed according to what makes sense for the function.

If the function takes a primitive type, pass by value would make sense. Some people I know would complain if it were passed by const ref (as it's 'unnecessary'), but I don't think I'd complain. If the function takes a user defined type and doesn't modify the parameter, then pass by const ref would make sense.

If it's a user defined type and the parameter is modified, then the semantics of the function would dictate how it should be passed.

like image 31
Michael Burr Avatar answered Sep 17 '22 15:09

Michael Burr