Converting data when output std container via ostream_iterator I have a strongly typed enum and std::vector of this type.

enum class Colors { red, green, blue };
std::vector v = { Colors::blue, Colors::red };


I trying to output v to standard output via std::copy.

std::copy(v.begin(), v.end(), std::ostream_iterator&lt;Colors&gt;(std::cout, " "); 


Compilation failed because compiler do not know how to print Colors. I can define operator &lt;&lt; for colors, but it looks too excessive for me. Then try print int values of v elements.

std::copy(v.begin(), v.end()
  , std::ostream_iterator&lt;std::underlying_type&lt;Colors&gt;::type&gt;&gt;(std::cout, " "); 


Compilation failed because it is prohibited to print Colors to std::ostream_iterator&lt;int&gt;. Ok. Then std::transform can help.

std::transform(v.begin(), v.end()
,   std::ostream_iterator&lt;std::underlying_type&lt;Colors&gt;::type&gt;(std::cout, " ")
,   [&amp;](Colors &amp;c) -&gt; std::underlying_type&lt;Colors&gt;::type
    {
        return std::underlying_type&lt;Colors&gt;::type(c);
    }
);


However, this lambda looks ugly. Is there neater way to copy or transform vector of Colors? Maybe boost has some way? 

Of course, a loop can print, but I'd prefer to avoid explicit loop.

for(auto c : v)
{
    std::cout &lt;&lt; std::underlying_type&lt;Colors&gt;::type(c) &lt;&lt; std::endl;
}




I have a lot of different enum classes in my project. All of them I need to print out. I don't like idea to create operator &lt;&lt; for each one individually. However, if I try to  make templated operator &lt;&lt; then I've got a global redefinition of this operator. And this is not what I actually want.

template &lt;typename T&gt;
std::ostream &amp;operator &lt;&lt; (std::ostream &amp;_os, const T &amp;_t) {
    _os &lt;&lt; typename std::underlying_type&lt;T&gt;::type(_t);
    return _os;
}
It seems to me that if you want to insert an object into a stream, the "right" way to do it is normally to define an operator&lt;&lt; for that type. That goes for strongly typed enums just as much as it does for class/struct objects.

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

enum class colors { red, green, blue };
std::vector&lt;colors&gt; v = { colors::blue, colors::red };

std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, colors const &amp;c) {
    return os &lt;&lt; std::underlying_type&lt;colors&gt;::type(c);
}

int main() {
    std:copy(v.begin(), v.end(), std::ostream_iterator&lt;colors&gt;(std::cout, "\n"));
}


That seems simple and straightforward enough that I'd be at least a little surprised to see a substantially cleaner/simpler solution (at least in C++ as it's currently defined; obviously, it's open to argument that some other language or some future definition of C++ could make things cleaner).

Using something like std::transform puts the burden in the wrong place. It requires that all code that uses the type in question contain and encode "knowledge" of the internals of the enumeration. What you generally want is to contain the complexity and the internals of the type in one place, so the rest of the code can just work with its external interface. An external interface of stream &lt;&lt; object is simple and straightforward. Forcing the rest of the world to work with an interface of stream &lt;&lt; std::underlying_type&lt;colors&gt;::type(c); anything but straightforward or simple. It's ugly and nasty.