Trying to create some gzip archive with a different filename inside I wrote this following snippet of code.
#include <iostream>
#include <utility>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/gzip.hpp>
boost::iostreams::filtering_ostream&& makeGZipStream(const std::string& archiveName,
const std::string& fileName)
{
boost::iostreams::filtering_ostream theGzipStream;
boost::iostreams::gzip_params theGzipParams;
theGzipParams.file_name = fileName;
theGzipStream.push(boost::iostreams::gzip_compressor{theGzipParams});
theGzipStream.push(boost::iostreams::file_sink{archiveName});
return std::move(theGzipStream);
}
int main()
{
boost::iostreams::filtering_ostream&& theGzipStream = makeGZipStream("archive.gz", "file");
theGzipStream << "This is a test..." << std::endl;
return 0;
}
This (as we may expect) produces a core dump because in makeGZipStream
we try to return a local stack-allocated variable by (rvalue-)reference. But the copy was not an option in this case as boost::iostreams::filtering_ostream
is non-copyable.
std::unique_ptr
"by value" thanks to its move constructor (and the move should not even occur in C++17 thanks to copy-elision), why isn't it possible in this case ?unique_ptr
(not so pretty)The compiler used was the quite old g++ (GCC) 4.9.3
.
Just return by value and remove std::move()
from return statement:
boost::iostreams::filtering_ostream makeGZipStream(const std::string& archiveName,
const std::string& fileName)
{
...
return theGzipStream;
}
if it fails to compile due to missing move constructor, then you are out of luck and this type is not movable, so wrap it into std::unique_ptr
and return that pointer by value (which is movable for sure).