在c++中,是通过值传递更好,还是通过引用到const传递更好?

我不知道哪种做法更好。我意识到,通过引用传递到const应该在程序中提供更好的性能,因为您没有对变量进行复制。


当前回答

这是我在设计非模板函数的接口时通常采用的方法:

Pass by value if the function does not want to modify the parameter and the value is cheap to copy (int, double, float, char, bool, etc... Notice that std::string, std::vector, and the rest of the containers in the standard library are NOT) Pass by const pointer if the value is expensive to copy and the function does not want to modify the value pointed to and NULL is a value that the function handles. Pass by non-const pointer if the value is expensive to copy and the function wants to modify the value pointed to and NULL is a value that the function handles. Pass by const reference when the value is expensive to copy and the function does not want to modify the value referred to and NULL would not be a valid value if a pointer was used instead. Pass by non-const reference when the value is expensive to copy and the function wants to modify the value referred to and NULL would not be a valid value if a pointer was used instead.

其他回答

听起来你得到答案了。传递值是昂贵的,但是如果需要的话,可以给您提供一个副本。

这取决于类型。这样就增加了必须进行引用和取消引用的小开销。对于大小等于或小于使用默认复制ctor的指针的类型,按值传递可能会更快。

根据经验,value用于非类类型,const引用用于类。 如果一个类非常小,最好是通过值传递,但区别很小。你真正想要避免的是按值传递一些巨大的类,并将其全部复制——如果你传递的是一个std::vector,其中包含相当多的元素,这将产生巨大的差异。

简单的区别:-在函数中,我们有输入和输出参数,所以如果你传递的输入和输出参数相同,那么使用引用调用,否则,如果输入和输出参数不同,那么最好使用值调用。

示例无效金额(int账户,int存款,int总数)

输入参数:账户、存款 输出参数:total

输入和输出是不同的使用值调用

无效金额(int total, int deposit)

投入总保证金 输出总

对于所有类型,除了内置类型(char, int, double等),迭代器和函数对象(lambdas,从std::*_function派生的类),通常推荐使用const ref。

在move语义出现之前尤其如此。原因很简单:如果通过值传递,则必须创建对象的副本,并且除了非常小的对象外,这总是比传递引用更昂贵。

在c++ 11中,我们获得了move语义。简而言之,move语义允许在某些情况下,对象可以“按值”传递,而无需复制它。特别是当你传递的对象是右值的时候。

就其本身而言,移动对象的代价至少与通过引用传递相同。然而,在许多情况下,函数无论如何都会在内部复制一个对象——即它将获得参数的所有权

在这些情况下,我们有以下(简化的)权衡:

我们可以通过引用传递对象,然后在内部复制。 我们可以按值传递对象。

“按值传递”仍然会导致对象被复制,除非该对象是右值。在右值的情况下,对象可以被移动,因此第二种情况突然不再是“复制,然后移动”,而是“移动,然后(可能)再次移动”。

对于实现了适当的move构造函数的大型对象(如向量、字符串……),第二种情况比第一种情况效率高得多。因此,如果函数拥有参数的所有权,并且对象类型支持有效移动,建议使用按值传递。


历史笔记:

事实上,任何现代编译器都应该能够计算出什么时候传递值是昂贵的,并在可能的情况下隐式地将调用转换为使用const ref。

理论上是这样。在实践中,编译器不能总是在不破坏函数的二进制接口的情况下更改此值。在一些特殊的情况下(当函数是内联的),如果编译器能够判断出原始对象不会通过函数中的操作被改变,副本实际上会被省略。

但一般来说,编译器不能确定这一点,而c++中move语义的出现使这种优化变得不那么重要。


例如,在Scott Meyers的《Effective c++》中。

2对于对象构造函数来说尤其如此,它可以接受参数并将它们存储在内部,作为构造对象状态的一部分。