Templated byte streaming I often have the need to take some type and stream it to something as a sequence of character byte values, so I came up with this to accomplish the task:

#include &lt;iostream&gt;
#include &lt;iterator&gt;

/// This provides a way of getting the byte representation of an
/// object of underlying type T.
///
/// This is an alternative to memcopying bytes into a string, stream,
/// character array...
template&lt;typename inT, typename outT = char&gt;
struct byte_streamer
{
    byte_streamer(inT ref)
      : data(ref)
    {}
    const inT data;
    outT placeholder;
};

template&lt;typename inT, typename outT&gt;
std::ostream&amp; operator&lt;&lt;(std::ostream&amp; strm, const byte_streamer&lt;inT, outT&gt; val)
{
    std::copy(reinterpret_cast&lt;const char*&gt;(&amp;val.data),
              reinterpret_cast&lt;const char*&gt;(&amp;val.data) + sizeof(inT),
              std::ostream_iterator&lt;decltype(val.placeholder)&gt;(strm, ""));
    return strm;
}

int main()
{
    std::cout &lt;&lt; "Here's the bytes of 42 copied directly: " &lt;&lt; byte_streamer(42) &lt;&lt; std::endl;
    std::cout &lt;&lt; "Here's the bytes of 42 copied as their numerical values: "
              &lt;&lt; byte_streamer&lt;int, int&gt;(42);
}


You can run it online on Coliru or just look at the output here:

Here's the bytes of 42 copied directly: *
Here's the bytes of 42 copied as their numerical values: 42000
Well, there are a few points:


You make a copy of the object you output. That can be costly, impossible, or have undesirable side-effects. Use a constant reference instead.
There are essentially two modes for your byte_streamer: Output the raw bytes, or output as decimal values for each byte.
The latter is broken as each byte is variable-width (1-3 digits for octets), and there is no separator.
You use the member placeholder only to convey the type outT to other code. Why not use a typedef instead, avoiding inefficiency and painful contortions?
Only use std::endl if you want to flush the stream. Otherwise, using '\n' and avoiding that extra-work is simply far more efficient.
It's really unusual not to end the programs output with a line-break. Wonder where the prompt will end up if called from a terminal...


Imho, the only useful outputs would be hex-dump and raw-bytes.

class dump_hex_t {
    const void* p = nullptr;
    std::size_t s = 0;
public:
    constexpr dump_hex_t() noexcept = default;
    constexpr dump_hex_t(const void* p, std::size_t s) noexcept : p(p), s(s) {}

    template &lt;class CharT, class Traits&gt;
    friend auto&amp; operator&lt;&lt;(std::basic_ostream&lt;CharT, Traits&gt;&amp; os, dump_hex_t x) {
        ... // Dump as hex-digits however you like
    }
};

template &lt;class T&gt;
constexpr dump_hex_t dump_hex(const T&amp; x) noexcept
{ return { std::addressof(x), sizeof x }; }

template &lt;class T&gt;
constexpr std::string_view dump_raw(const T&amp; x) noexcept
{ return { (const char*)std::addressof(x), sizeof x }; }