PrintLn header for C++ I've always wondered what's the most elegant way of implementing PrintLn in C++. I have not yet come with perfect conclusion. This is my shot.

Shortcut for std::cout, std::cerr and std::clog "print line" version. So you can type 
CoutLn &lt;&lt; "Hello world!"; instead of std::cout &lt;&lt; "Hello world!" &lt;&lt; '\n';

CoutLn, CerrLn, ClogLn are implemented.

#include &lt;iostream&gt;
#include &lt;typeinfo&gt;
#include &lt;mutex&gt;

/* Template magic: determine if our type is printable. */
template&lt;typename S, typename T, typename = void&gt;
struct is_to_stream_writable : std::false_type {};
template&lt;typename S, typename T&gt;
struct is_to_stream_writable&lt;S, T, std::void_t&lt;decltype(std::declval&lt;S&amp;&gt;()&lt;&lt;std::declval&lt;T&gt;())&gt;&gt; : std::true_type {};

/* Since std::cout, std::cerr etc. are all different instances of the same type, 
 * let's define dummy classes for detecting different instances. */
class Cout{};
class Cerr{};
class Clog{};

template&lt;class Stream = Cout&gt;
class PrintLn {
  private:
    static std::ostream* stream;
    static std::mutex m;
  public:
    PrintLn() {
      if constexpr (std::is_same&lt;Stream, Cout&gt;::value) stream = &amp;std::cout;
      else if constexpr (std::is_same&lt;Stream, Cerr&gt;::value) stream = &amp;std::cerr;
      else if constexpr (std::is_same&lt;Stream, Clog&gt;::value) stream = &amp;std::clog;
    }
    template&lt;class T&gt;
    PrintLn&amp; operator&lt;&lt;(const T&amp; msg) {
      static_assert(is_to_stream_writable&lt;std::ostream, T&gt;::value, "your type is not printable");
      std::lock_guard&lt;std::mutex&gt;(PrintLn&lt;Stream&gt;::m);
      *stream &lt;&lt; msg;
      return *this;
    }
    ~PrintLn() {
      std::lock_guard&lt;std::mutex&gt;(PrintLn&lt;Stream&gt;::m);
      *stream &lt;&lt; '\n';
    }
};

/* Declare static variables. */
template&lt;class Stream&gt;
typename::std::mutex PrintLn&lt;Stream&gt;::m;

template&lt;class Stream&gt;
typename::std::ostream* PrintLn&lt;Stream&gt;::stream;

using CoutLn = PrintLn&lt;Cout&gt;;
using CerrLn = PrintLn&lt;Cerr&gt;;
using ClogLn = PrintLn&lt;Clog&gt;;

/* Shorter syntax, for example: `CerrLn{} &lt;&lt; "error";` -&gt; `CerrLn &lt;&lt; "error";`. */
#define CoutLn CoutLn{}
#define CerrLn CerrLn{}
#define ClogLn ClogLn{}
Have you looked at C++20 osyncstream?  It seems to have a better interface.

Multithreading

std::lock_guard&lt;std::mutex&gt;(PrintLn&lt;Stream&gt;::m);


This line is useless, because temporary objects are destroyed at the end of the full-expression.  (Does this even have a temporary in C++17?)  You need a named variable instead.  Also make use of class template argument deduction:

std::lock_guard lock{m};


Also,

Streams

Your approach is unnecessarily restricted because only three hardcoded streams std::cout, std::cerr, and std::clog are supported.  And I think {} is OK and these don't really help a lot:

#define CoutLn CoutLn{}
#define CerrLn CerrLn{}
#define ClogLn ClogLn{}


Make the function object have regular semantics instead.  You may use a hash map internally to store the mutexes, as syncbuf does.

Also SFINAE on operator&lt;&lt;.



Here's the same thing implemented with osyncstream:

template &lt;
    class CharT,
    class Traits = std::char_traits&lt;CharT&gt;,
    class Allocator = std::allocator&lt;CharT&gt;
&gt; class PrintLn : public std::basic_osyncstream&lt;CharT, Traits, Allocator&gt; {
    using Base = std::basic_osyncstream&lt;CharT, Traits, Allocator&gt;;
public:
    using Base::Base;

    PrintLn(PrintLn&amp;&amp;) = default;
    PrintLn&amp; operator=(PrintLn&amp;&amp;) = default;
    ~PrintLn()
    {
        if (this-&gt;get_wrapped()) {
            *static_cast&lt;Base*&gt;(this) &lt;&lt; '\n';
        }
    }
};

inline auto cout_ln()
{
    return PrintLn{std::cout};
}
inline auto cerr_ln()
{
    return PrintLn{std::cerr};
}
inline auto clog_ln()
{
    return PrintLn{std::clog};
}


But anyway, why would you wanna do this when you can simply print a \n?