Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I let the C++ compiler decide whether to pass-by-value or pass-by-reference?

Have a look at this hypothetical header file:

template <class T>
class HungryHippo {
public:
    void ingest(const T& object);
private:
    ...
}

Now, for a HungryHippo<string> it makes sense that you would want to ingest references to the strings -- copying a string might be very expensive! But for a HungryHippo<int> it makes way less sense. Passing an int directly can be really cheap (most compilers will do it in a register), but passing a reference to an int is an extra needless level of indirection. This all applies to returning values as well.

Is there some way to suggest to the compiler "hey, I'm not going to modify the argument, so you decide whether to pass by value or by reference, depending on what you think is better"?

Some things that may be relevant:

  • I can fake this effect manually by writing template <class T, bool PassByValue> class HungryHippo and then specializing on PassByValue. If I wanted to get really fancy, I could even infer PassByValue based on sizeof(T) and std::is_trivially_copyable<T>. Either way, this is a lot of extra work when the implementations are going to look pretty much the same, and I suspect the compiler can do a much better job of deciding whether to pass by value than I can.
  • The libc++ project seems to solve this by inlining a lot of functions so the compiler can make the choice one level up, but in this case let's say the implementation of ingest is fairly complicated and not worth inlining. As explained in the comments, all template functions are inline by default.
like image 306
Calvin Avatar asked Nov 16 '12 00:11

Calvin


People also ask

Does C allow pass by reference?

Pass by Reference A reference parameter "refers" to the original data in the calling function. Thus any changes made to the parameter are ALSO MADE TO THE ORIGINAL variable. Arrays are always pass by reference in C.

Is C language pass by value or reference?

C always uses 'pass by value' to pass arguments to functions (another term is 'call by value', which means the same thing), which means the code within a function cannot alter the arguments used to call the function, even if the values are changed inside the function.

Should you pass reference or value?

When passing by reference you are basically passing a pointer to the variable. Pass by value you are passing a copy of the variable. In basic usage this normally means pass by reference, changes to the variable will seen be in the calling method and in pass by value they won't.

Should I always use pass by reference?

Always passing a reference and putting const in front of it is tedious and prone to errors. Also, always passing references would mean creating a local variable for each function argument (instead of passing expressions).


1 Answers

The boost::call_traits header deals with exactly this issue. Check it out here.

Specifically, the call_traits<T>::param_type option includes the following description:

If T is a small built in type or a pointer, then param_type is defined as T const, instead of T const&. This can improve the ability of the compiler to optimize loops in the body of the function if they depend upon the passed parameter, the semantics of the passed parameter is otherwise unchanged (requires partial specialization).

In your case, you could define ingest as follows:

template <class T>
class HungryHippo {
public:
    void ingest(call_traits<T>::param_type object);
    // "object" will be passed-by-value for small 
    // built-in types, but passed as a const reference 
    // otherwise
private:
    ...
};

Whether this would actually make much of a difference in your actual code/compiler combination, I'm not sure. As always, you'd have to run some actual benchmarks and see what happens...

like image 54
Darren Engwirda Avatar answered Sep 28 '22 17:09

Darren Engwirda