我最近听了Herb Sutter的一个演讲,他认为通过const &传递std::vector和std::string的原因已经基本消失了。他建议现在最好编写如下这样的函数:

std::string do_something ( std::string inval )
{
   std::string return_val;
   // ... do stuff ...
   return return_val;
}

我知道return_val在函数返回时将是一个右值,因此可以使用move语义返回,这非常便宜。然而,inval仍然比引用(通常实现为指针)的大小大得多。这是因为std::string有各种组件,包括指向堆的指针和用于短字符串优化的成员char[]。所以在我看来,通过引用传递仍然是一个好主意。

谁能解释一下赫伯为什么会这么说?


当前回答

这在很大程度上取决于编译器的实现。

然而,这也取决于你使用什么。

让我们考虑下一个函数:

bool foo1( const std::string v )
{
  return v.empty();
}
bool foo2( const std::string & v )
{
  return v.empty();
}

为了避免内联,这些函数在单独的编译单元中实现。然后: 1. 如果将一个字面值传递给这两个函数,将不会看到性能上的太大差异。在这两种情况下,都必须创建一个字符串对象 2. 如果传递另一个std::string对象,foo2将优于foo1,因为foo1将进行深度复制。

在我的PC上,使用g++ 4.6.1,我得到了这些结果:

参考变量:1000000000次迭代——>时间流逝:2.25912秒 变量值:1000000000次迭代—>时间流逝:27.2259秒 参考文字:100000000次迭代——>时间流逝:9.10319秒 字面值:100000000次迭代——>时间流逝:8.62659秒

其他回答

我复制/粘贴了这个问题的答案,并更改了名称和拼写以适应这个问题。

下面是用来衡量问题的代码:

#include <iostream>

struct string
{
    string() {}
    string(const string&) {std::cout << "string(const string&)\n";}
    string& operator=(const string&) {std::cout << "string& operator=(const string&)\n";return *this;}
#if (__has_feature(cxx_rvalue_references))
    string(string&&) {std::cout << "string(string&&)\n";}
    string& operator=(string&&) {std::cout << "string& operator=(string&&)\n";return *this;}
#endif

};

#if PROCESS == 1

string
do_something(string inval)
{
    // do stuff
    return inval;
}

#elif PROCESS == 2

string
do_something(const string& inval)
{
    string return_val = inval;
    // do stuff
    return return_val; 
}

#if (__has_feature(cxx_rvalue_references))

string
do_something(string&& inval)
{
    // do stuff
    return std::move(inval);
}

#endif

#endif

string source() {return string();}

int main()
{
    std::cout << "do_something with lvalue:\n\n";
    string x;
    string t = do_something(x);
#if (__has_feature(cxx_rvalue_references))
    std::cout << "\ndo_something with xvalue:\n\n";
    string u = do_something(std::move(x));
#endif
    std::cout << "\ndo_something with prvalue:\n\n";
    string v = do_something(source());
}

对我来说,这输出:

$ clang++ -std=c++11 -stdlib=libc++ -DPROCESS=1 test.cpp
$ a.out
do_something with lvalue:

string(const string&)
string(string&&)

do_something with xvalue:

string(string&&)
string(string&&)

do_something with prvalue:

string(string&&)
$ clang++ -std=c++11 -stdlib=libc++ -DPROCESS=2 test.cpp
$ a.out
do_something with lvalue:

string(const string&)

do_something with xvalue:

string(string&&)

do_something with prvalue:

string(string&&)

下表总结了我的结果(使用clang -std=c++11)。第一个数字是复制结构的数量,第二个数字是移动结构的数量:

+----+--------+--------+---------+
|    | lvalue | xvalue | prvalue |
+----+--------+--------+---------+
| p1 |  1/1   |  0/2   |   0/1   |
+----+--------+--------+---------+
| p2 |  1/0   |  0/1   |   0/1   |
+----+--------+--------+---------+

值传递解决方案只需要一次重载,但在传递左值和x值时需要额外的move构造。对于任何特定的情况,这可能是可接受的,也可能是不可接受的。这两种解决方案各有优缺点。

是传递const std::string &作为参数的日子?

不。许多人采纳了这个建议(包括Dave Abrahams),并将其简化为适用于所有std::string参数——始终按值传递std::string对于任何和所有任意参数和应用程序都不是“最佳实践”,因为这些演讲/文章关注的优化只适用于有限的一组情况。

如果要返回值、改变参数或获取值,那么按值传递可以节省昂贵的复制,并提供语法上的便利。

与以往一样,当您不需要拷贝时,传递const引用可以节省大量复制。

现在来看看具体的例子:

然而,inval仍然比引用(通常实现为指针)的大小大得多。这是因为std::string有各种组件,包括指向堆的指针和用于短字符串优化的成员char[]。所以在我看来,通过引用传递仍然是一个好主意。谁能解释一下赫伯为什么会这么说?

如果考虑到堆栈大小(并且假设这不是内联/优化的),return_val + inval > return_val——IOW,可以通过在这里传递值来降低堆栈使用的峰值(注意:ABIs的过度简化)。同时,通过const引用传递可以禁用优化。这里的主要原因不是为了避免堆栈增长,而是为了确保优化可以在适用的地方执行。

通过const引用传递的日子并没有结束——规则只是比以前更复杂了。如果性能很重要,明智的做法是根据实现中使用的细节考虑如何传递这些类型。

Herb Sutter和Bjarne Stroustroup一起推荐使用const std::string&作为形参类型;见https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rf-in。

这里有一个在其他答案中没有提到的陷阱:如果你将一个字符串字面值传递给一个const std::string& parameter,它将传递一个临时字符串的引用,该字符串是动态创建的,用于保存字面值的字符。如果然后保存该引用,那么一旦释放临时字符串,它将无效。为了安全起见,您必须保存副本,而不是参考资料。这个问题源于字符串字面值是const char[N]类型,需要升级为std::string。

下面的代码说明了陷阱和解决方法,以及一个较小的效率选项——使用const char*方法重载,如在c++中是否有一种方法将字符串文字作为引用传递。

(注意:Sutter & Stroustroup建议,如果你保留了字符串的副本,也要提供一个带有&&形参和std::move()的重载函数。)

#include <string>
#include <iostream>
class WidgetBadRef {
public:
    WidgetBadRef(const std::string& s) : myStrRef(s)  // copy the reference...
    {}

    const std::string& myStrRef;    // might be a reference to a temporary (oops!)
};

class WidgetSafeCopy {
public:
    WidgetSafeCopy(const std::string& s) : myStrCopy(s)
            // constructor for string references; copy the string
    {std::cout << "const std::string& constructor\n";}

    WidgetSafeCopy(const char* cs) : myStrCopy(cs)
            // constructor for string literals (and char arrays);
            // for minor efficiency only;
            // create the std::string directly from the chars
    {std::cout << "const char * constructor\n";}

    const std::string myStrCopy;    // save a copy, not a reference!
};

int main() {
    WidgetBadRef w1("First string");
    WidgetSafeCopy w2("Second string"); // uses the const char* constructor, no temp string
    WidgetSafeCopy w3(w2.myStrCopy);    // uses the String reference constructor
    std::cout << w1.myStrRef << "\n";   // garbage out
    std::cout << w2.myStrCopy << "\n";  // OK
    std::cout << w3.myStrCopy << "\n";  // OK
}

输出:

Const char *构造函数 常量std::string&构造函数 第二个字符串 第二个字符串

这在很大程度上取决于编译器的实现。

然而,这也取决于你使用什么。

让我们考虑下一个函数:

bool foo1( const std::string v )
{
  return v.empty();
}
bool foo2( const std::string & v )
{
  return v.empty();
}

为了避免内联,这些函数在单独的编译单元中实现。然后: 1. 如果将一个字面值传递给这两个函数,将不会看到性能上的太大差异。在这两种情况下,都必须创建一个字符串对象 2. 如果传递另一个std::string对象,foo2将优于foo1,因为foo1将进行深度复制。

在我的PC上,使用g++ 4.6.1,我得到了这些结果:

参考变量:1000000000次迭代——>时间流逝:2.25912秒 变量值:1000000000次迭代—>时间流逝:27.2259秒 参考文字:100000000次迭代——>时间流逝:9.10319秒 字面值:100000000次迭代——>时间流逝:8.62659秒

IMO使用c++引用std::string是一个快速而简短的局部优化,而使用传递值可能是(或不是)一个更好的全局优化。

所以答案是:这取决于环境:

如果你把所有的代码从外部写到内部函数,你知道代码是做什么的,你可以使用引用const std::string &。 如果您编写库代码,或者在传递字符串的地方大量使用库代码,那么通过信任std::string复制构造函数行为,您可能会获得更多全局意义上的好处。