Pass by reference or not?

I was wondering what would be the correct approuch to a function.

Assuming there are no changes made to the arguments passed:

type function(const type &arg)

or just:

type function(type arg)

and let the compiler make a new copy of the argument that we will never use?

A concrete example:

1
2
void print_int(int a)
{cout<<a;}


or

1
2
void print_int(const int &a)
{cout<<a;}


What's the best approuch?

EDIT: Typos..
Last edited on
If it's a builtin (int, double, ...) or small type with a trivial copy constructor, pass by value.
If it's a more complex type, passing by reference is more advantageous.
If you're gonna make a copy of the value in the function, pass by value and use std::move.
So pass by reference in the said case, can only be beneficial, right?

Although the benefits might be very small and/or unnouticeable, there is no negative consequence in doing so or is it?

Thank's in advance,

Hugo Ribera
When to pass parameters by value, reference, and pointer
http://www.cplusplus.com/articles/z6vU7k9E/
Topic archived. No new replies allowed.