c++ - 为什么 C++ 允许返回 ifstream 对象?
问题描述
在 C++98 中,以下代码无法编译,因为 ifstream 没有复制构造函数:
#include <iostream>
#include <fstream>
using namespace std;
ifstream f() {
return ifstream("main.cpp");
}
int main() {
ifstream st= f();
}
但是,在 C++11 中使用多个 GCC 版本,编译时不会出现警告。这是什么原因?
解决方案
C++11 添加了移动构造函数。流现在已移动。st
这里的源对象是返回表达式中的一个临时对象,可以移动到main
.
推荐阅读
- c# - 我不知道如何在 C# 中使我的菜单循环脱离它的选择
- terraform - 我什么时候应该在 Terraform 资源模式中使用复合 ID 与属性/块?
- python - 在扑克牌列表中寻找对子
- javascript - 查找所有匹配项并从数组对象返回值
- machine-learning - 如何在 FeatureUnion 中对 scikit 转换器进行特征选择
- google-cloud-platform - GCP 组织管理员缺少权限
- android - 在 Android 中获取当前工作的主要网络接口
- android - 无法解决构建失败
- postgresql - -bash: /usr/bin/psql: 权限被拒绝
- python - 尝试写入 CSV 文件但收到错误消息:“_csv.writer”对象不可迭代