看看之前的答案,似乎虽然std::ostream不是可移动的,但std::ofstream应该是.但是,这段代码
#include <fstream>
int main()
{
std::ofstream ofs;
std::ofstream ofs2{std::move(ofs)};
}
Run Code Online (Sandbox Code Playgroud)
似乎没有在我试过的任何版本的gcc或clang中编译(使用--std = c ++ 11或--std = c ++ 14).编译器错误有所不同,但这是我得到的gcc 4.9.0
6 : error: use of deleted function 'std::basic_ofstream::basic_ofstream(const std::basic_ofstream&)'
Run Code Online (Sandbox Code Playgroud)
根据标准,这是预期的行为吗?
请注意,之前问过一个非常类似的问题(std :: ofstream是否可以移动?)但是从那时起标准似乎发生了变化(详见 为什么不能移动std :: ostream?)使这些答案过时.当然,这些答案都没有解释为什么上面的代码不能编译.
在尝试使用容器时遇到了这个问题ofstream,因为上述情况不起作用.