c++ - 是否可以通过引用返回一个临时对象

标签 c++

是否可以像示例代码中那样从函数返回引用:

string &erase_whitespace(string &text)
{
    text.erase(**etc.**);
    return text;
}

调用:

string text = erase_whitespace(string("this is a test"));
cout << test;

这段代码有效吗?在 Visual C++ 上,它不会崩溃,但看起来不对。

谢谢

最佳答案

来自 C++ 2003 标准(草案)的 § 12.2.3

Temporary objects are destroyed as the last step in evaluating the full-expression (1.9) that (lexically) contains the point where they were created.

§ 12.2.4:

There are two contexts in which temporaries are destroyed at a different point than the end of the full- expression. ...

§ 12.2.5:

The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. ... A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call.

§8.5.3.5 决定了何时引用必须是 const 类型。如果临时对象是具有返回适当引用的转换运算符的类的实例(那是一口),则临时对象可能会绑定(bind)到非常量引用。一个例子可能更容易理解:

class Foo {
    ...
    operator Bar&() const;
...
void baz(Bar &b);
...
    baz(Foo()); // valid
    baz(Bar()); // not valid

最后一行无效,因为第 12.3.2.1 节指出“转换函数永远不会用于将 [an ...] 对象转换为 [...] 相同的对象类型(或对 it)”。您可以通过 Bar 的祖先和虚拟转换函数使用强制转换来使其工作。

赋值是一个表达式(第 5.17 节),因此代码中的完整表达式(第 1.9.12 节)就是赋值。这给出了以下序列(暂时忘记临时字符串可能无法绑定(bind)到非常量引用):

  1. 创建一个临时字符串
  2. 临时绑定(bind)到 erase_whitespacestring& text 参数上
  3. erase_whitespace 发挥作用。
  4. erase_whitespace 返回对临时文件的引用
  5. 临时复制到string text
  6. 临时文件被销毁。

所以在这种情况下一切都是犹太洁食。正如 Mike Seymour 指出的那样,问题案例是将 erase_whitespace 的结果分配给一个引用。请注意,这可能不会立即导致问题,因为存储字符串的区域可能包含与临时对象被销毁之前相同的数据。然而,下次在堆栈或堆上分配某些东西时......

关于c++ - 是否可以通过引用返回一个临时对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1714874/

相关文章:

c# - C++ 等价于 C# 的内部

c++ - 如何为 =0 创建 C++ 赋值运算符

c++ - 包含 .h 文件时出现链接器错误,但包含对应的 .cpp 文件时链接器错误消失

c++ - 虚基类初始化

C++ vector 对初始化

c++ - 包含循环类以保持 C++ 的向后兼容性

c++ - c++ 中的 sqlite - 来自不同应用程序的并行插入,会发生什么?

c++ - 用 C++ 绘制图形

c++ - 使用系统设置图标

c# 非阻塞接收套接字与 try-catch SocketException,性能