Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there any specific case where pass-by-value is preferred over pass-by-const-reference in C++?

Tags:

c++

I read that they are conceptually equal. In practice, is there any occasion that

foo(T t) 

is preferred over

foo(const T& t)

? and why?


Thanks for the answers so far, please note I am not asking the difference between by-ref and by-val.

Actually I was interested in the difference between by-const-ref and by-val.

I used to hold the oipinion that by-const-ref can replace by-value in call cases since even Herb Sutter and Bjarne said they are conceptually equal, and "by ref"(be it const) implies being faster. until recently, I read somewhere that by-val may be better optimized in some cases.

Then when and how?

like image 357
t.g. Avatar asked May 22 '09 16:05

t.g.


People also ask

Which is better pass by value or pass by reference?

Pass-by-references is more efficient than pass-by-value, because it does not copy the arguments. The formal parameter is an alias for the argument. When the called function read or write the formal parameter, it is actually read or write the argument itself.

What is the difference between pass by reference and pass by const reference?

From what I understand: when you pass by value, the function makes a local copy of the passed argument and uses that; when the function ends, it goes out of scope. When you pass by const reference, the function uses a reference to the passed argument that can't be modified.

What is one advantage of passing by reference over passing by value?

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.

When should structures be passed by values or by reference?

Always pass by reference, unless you need a copy, otherwise you are guilty of peeking inside an object, examining it's implementation and deciding that pass-by-value is preferred for some reason. Because pass-by-value is the default semantic of any programming language that is written in terms of function calls.


2 Answers

Built-in types and small objects (such as STL iterators) should normally be passed by value.

This is partly to increase the compiler's opportunities for optimisation. It's surprisingly hard for the compiler to know if a reference parameter is aliasing another parameter or global - it may have to reread the state of the object from memory a number of times through the function, to be sure the value hasn't changed.

This is the reason for C99's restrict keyword (the same issue but with pointers).

like image 70
James Hopkin Avatar answered Oct 24 '22 15:10

James Hopkin


If you want to locally modify t (without affecting the original) in the body of your method (say in the process of calculating something), the first method would be preferential.

like image 32
Paul Sonier Avatar answered Oct 24 '22 13:10

Paul Sonier