Alex Rivera | Logout

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

Asked 2009-04-06T16:44:20.260
30

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?

Edit
Report

1 Answer

20

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.

answered 2009-04-06T17:00:04.123

Your Answer