#include <iostream>
#include <fstream>
class A {
private:
std::ostream&& out;
public:
A(std::ostream&& o) : out(std::move(o)) {
out.write("test", 4);
}
void writeTest2() {
out.write("test2", 5);
out.flush(); // still does nothing.
}
};
int main() {
A a{std::ofstream{"testic"}};
a.writeTest2();
}
When the above code is run, it creates a file named testic
as expected. However, the file created contains test
but not testtest2
, which is obviously unexpected. What exactly is causing this behavior?
When std::ostream
is taken as a lvalue reference it functions perfectly as intended.
Additional information
- Compilers tried:
clang
,gcc
. - On Platform: Linux (4.19.0-11-amd64).