Guys would it be difficult to write coutn which would basically place newline symbol at the end of the input. While working with console (that's all I can do at the moment) I'm finding very tedious to write '\n' every time I want the line to be a new line.
Or maybe it is already implemented?
views:
450answers:
5The way that streams work is that they add one thing and then return itself.
so are you sure you'd want to do what you're asking?
For example:
s << "a" << "b";
Would put:
a
b
Surely you can find a way with some kind of wrapper but the extra overhead incurred and the diminished code readability I don't think is worth it. Most coders are familiar with standard C++. Most coders are not familiar with your coutn and how it works.
Roughly:
struct StreamFilterN
{
ostream & os;
StreamFilterN(ostream & os_) : os(os_) {}
template <typename T>
StreamFilterN & operator<<(T const & x) { os << x << "\n"; return *this; }
}
StreamFilterN coutn(cout);
coutn << "Hello world";
coutn << "Yeah, like.";
(I'm not sure if this works perfectly, but it should get you started)
Well, the first obvious thing would be to implement some kind of wrapper around it:
class Print
{
public:
explicit Print(std::ostream& stream = std::cout): mStream(stream) {}
~Print() { mStream << '\n'; }
template <class T>
Print& operator<<(T const& t) { mStream << t; return *this; }
private:
std::ostream& mStream;
};
Usage is like such:
int main(int argc, char* argv[])
{
Object object;
Print() << "My new object value: " << object;
std::ofstream file("file.log");
Print(file) << "My new object value: " << object;
}
Easy, Peasy.
To circumvent the multiple injections on a single line, you could use a temporary object. This temporary object would add the '\n' in its destructor.
struct coutn {
coutn(): os(cout) {}
~coutn() { os << '\n'; }
template <typename T>
coutn & operator<<(T const & x) { os << x; return *this; }
private:
ostream &os;
};
coutn() << "Hello " << "World" << "!";
In the end, I'm wondering if this coutn is actually better?
Come one people. You must admit that C++'s newlines are too verbose. Look at C#, D, Java, Python, Ruby, should I go on? I don't think you should implement a coutn. I think you'd be better off following D's example with write, writeln, writef, and writefln variadic template functions.
Anyway here is how to do it without macros and with out needing to explicitly create temporaries.
class coutn_t
{
private:
class newliner
{
public:
explicit newliner(std::ostream & out=std::cout) : m_out(out) { }
template<typename T>
newliner& operator<<( const T& t)
{
m_out << t;
return *this;
}
~newliner() { m_out << std::endl; }
private:
std::ostream & m_out;
};
public:
// NOTE: these return by value. Only one newliner will be
// destructed however as RVO will eliminate the copy.
newliner operator()( std::ostream& out )
{ return newliner(out); }
template< typename T >
newliner operator<<( const T& t )
{
std::cout << t;
return newliner(std::cout);
}
};
coutn_t coutn;
coutn << "Hello World!";
coutn << "Hello " << "World" << '!';
std::ofstream file("ouput.txt");
coutn(file) << "Hello World" << " via file";
The complete solution will have to add more overloads for operator<< to handle stream manipulators (std::hex etc). The functions returning newliners must always be inline to allow RVO.