vendredi 27 février 2015

Why can't I move std::ofstream?

Looking at previous answers on SO, it seems that while std::ostream is not be movable, std::ofstream should be. However, this code



#include <fstream>

int main()
{
std::ofstream ofs;
std::ofstream ofs2{std::move(ofs)};
}


does not seem to compile in any version of gcc or clang I tried (with either --std=c++11 or --std=c++14). The compiler error varies somewhat, but here's what I get for gcc 4.9.0



6 : error: use of deleted function 'std::basic_ofstream::basic_ofstream(const std::basic_ofstream&)'


Is this the expected behavior, according to the standard?


Note that a very similar question was asked before ( Is std::ofstream movable? ) but seems the standard has changed since then ( as detailed in Why can't std::ostream be moved? ) rendering those answers outdated. Certainly, none of those answers explains why the code above does not compile.


Came across this issue when trying to use containers of ofstream, which does not work because of the above.


Aucun commentaire:

Enregistrer un commentaire