【发布时间】:2021-04-13 14:13:40
【问题描述】:
在 C++98 中,以下代码无法编译,因为 ifstream 没有复制构造函数:
#include <iostream>
#include <fstream>
using namespace std;
ifstream f() {
return ifstream("main.cpp");
}
int main() {
ifstream st= f();
}
但是,在 C++11 中使用多个 GCC 版本时,编译时不会出现警告。这是什么原因?
【问题讨论】:
-
RVO / 复制省略:stackoverflow.com/questions/12953127/…
-
@HattedRooster:保证 RVO 是 C++17 的东西,而不是这个问题中的 C++11。
-
@HattedRooster 应用 RVO 时,C++11 中仍需要复制/移动构造函数。这是由 C++98 中不存在的附加移动构造函数引起的。