Reputation: 1123
I don't know how to "buffer" a line of input (and perform some operations on that line, e.g. insert a newline at the end) before the next one in the following code:
#include <iostream>
class Test {
public:
template<class T>
Test& operator<< (T&& anything) {
std::cout << anything;
return *this;
}
};
int main() {
Test myobj;
myobj << "hello" << "this is cool";
// How to insert a newline here?
myobj << "but" << "this is NOT cool";
}
I'd like to be able to detect when the line
myobj << "hello" << "this is cool";
has completed before the next one is executed.
Upvotes: 1
Views: 58
Reputation: 96800
You can detect it in its destructor. Create a log()
helper function that returns a temporary. At the end of the full expression its destructor will run:
class Test {
public:
template<class T>
Test& operator<< (T&& anything) {...}
~Test() { std::cout << std::endl; }
};
Test log() { return Test(); }
// ...
log() << "hello" << "this is cool"; // destructor inserts a newline here
log() << "but" << "this is NOT cool"; // here too
Upvotes: 0
Reputation: 15824
`"\n"`
do it for you as shown below:
int main() {
Test myobj;
myobj<< "hello" << "this is cool<<"\n"";
// How to insert a newline here?
myobj << "but" << "this is NOT cool";
}
Or otherwise you use std::endl
as follows
myobj << "hello" << "this is cool<<std::endl;
Upvotes: 1