3
votes
std::map<std::string, std::ofstream> Map;
std::string name="name";
std::ofstream ofs(name,std::ios::app);
Map[name] = std::move(ofs);

I run the code above but it failed. I compiled it by g++ 4.9 on Ubuntu12.04 and g++-5 (gcc version 5.4.1 20160904 (Ubuntu 5.4.1-2ubuntu1~12.04) ) using -std=c++11, which induces the same error message below.

error: use of deleted function ‘std::basic_ofstream& std::basic_ofstream::operator=(const std::basic_ofstream&)’ Map[name] = std::move(ofs);

/usr/include/c++/4.9/fstream:602:11: note: ‘std::basic_ofstream& std::basic_ofstream::operator=(const std::basic_ofstream&)’ is implicitly deleted because the default definition would be ill-formed: class basic_ofstream : public basic_ostream<_CharT,_Traits>

1
Looks to be g++ version-specific. Does build with VS 2013 and g++-5.1.acraig5075

1 Answers

4
votes

Support for moving iostreams was added to GCC 5.1, so you can't do it with GCC 4.9. This is documented in the libstdc++ manual for version 4.9: https://gcc.gnu.org/onlinedocs/gcc-4.9.4/libstdc++/manual/manual/status.html#status.iso.2011

27.5 | Iostreams base classes | Partial | Missing move and swap operations on basic_ios. Missing io_errc and iostream_category. ios_base::failure is not derived from system_error. Missing ios_base::hexfloat.
27.6 | Stream buffers | Y |
27.7 | Formatting and manipulators | Partial | Missing move and swap operations Missing get_time and put_time manipulators.
27.8 | String-based streams | Partial | Missing move and swap operations
27.9 | File-based streams | Partial | Missing move and swap operations

It is supported in GCC 5.x and works fine, so you must be doing something wrong (probably forgetting to use -std=c++11, or pointing it to the 4.9 headers which definitely won't work).