これはちょっと難しい問題です。std::cout に似た外観にしようとしているロギング関数を書いています
したがって、基本的には、次のコードをコメントどおりに実行することが目標です
log << "text"; // This would output "text\n";
log << "some " << "text"; //This would output "some text\n"
log << "some number "<< 5; //This would output "some number 5\n"
可変個引数テンプレートを使用してみましたが、うまくいきませんでした。これまでで最も成功した試みは次のとおりです。
#include <iostream>
// stream_newliner works by just taking the address of an ostream you
// give it, and forwarding all writes (via operator<<) to that ostream.
// Then, when it finally destructs, it writes the newline.
template <typename CharT, typename Traits>
class stream_newliner
{
public:
// The constructor just takes the address of the stream you give it.
explicit stream_newliner(std::basic_ostream<CharT, Traits>& s) :
p_s_{&s}
{}
// Boilerplate move construction. Nothing special here.
stream_newliner(stream_newliner&& s) : p_s_{nullptr}
{
*this = std::move(s);
}
// On destruction, write the newline. (Note we check the pointer for
// null for technical reasons.)
~stream_newliner()
{
if (p_s_)
{
// If you have std::unhandled_exceptions(), you can check to
// see if a new exception is in flight, and only do the
// newline if there's not.
// Note that I'm writing an X and not a newline so you can
// see it easier.
(*p_s_) << 'X';
}
}
// This is where the magic happens. Any time you do "x << y" where
// x is this type, it just passes it on to the underlying stream.
// When you do "x << y1 << y2", it all works magically because it
// translates to "operator<<(operator<<(x, y1), y2)"... and this
// function just passes each call to the underlying stream.
template <typename T>
auto operator<<(T&& t) -> stream_newliner<CharT, Traits>&
{
(*p_s_) << std::forward<T>(t);
return *this;
}
// Boilerplate move assignment. Nothing special here.
auto operator=(stream_newliner&& s) -> stream_newliner&
{
std::swap(p_s_, s.p_s_);
return *this;
}
// Technically don't need these, but it doesn't hurt to be explicit.
stream_newliner(stream_newliner const&) = delete;
auto operator=(stream_newliner const&) = delete;
private:
// You could use a reference to the stream rather than a pointer,
// but references are harder to work with as class members.
std::basic_ostream<CharT, Traits>* p_s_ = nullptr;
};
// Helper function to make it easier to create.
template <typename CharT, typename Traits>
auto line(std::basic_ostream<CharT, Traits>& s)
{
return stream_newliner<CharT, Traits>{s};
}
auto main() -> int
{
line(std::cout) << "foo";
std::cout << '\n';
line(std::cout) << 1 << ' ' << 2 << ' ' << 3;
std::cout << '\n';
}