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

后端 未结 15 1925
臣服心动
臣服心动 2020-12-06 08:03

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

foo(T t) 

is preferred over

foo(const T&         


        
15条回答
  •  被撕碎了的回忆
    2020-12-06 08:28

    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.

提交回复
热议问题