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?
The reason pass by const reference and by value are conceptually the same is that neither can modify the original.
Normally, I am big fan of pass by value because it creates code that avoids many of the complexities that occur when multiple threads are sharing access to common data.
That said, it does potentially make you're code slower. My take in the past has always been to prefer pass by value unless I know their is (or will be) a performance problem by doing so. I may have to modify this slightly to include pass by const reference as an even better option.
In addition, foo(T t) is normally used when T is a simple type (int, bool, etc).