Sorry, I left then without seeing your answer.
I think your answer and mine are equally patchy but both solve the original
problem .. almost (see below). My code (here and below I mean OP's issue only)
explicitly use value_type and both mine and your code implicitly use
const_iterator; each would make Collection deducible by expressing only one of
the container requirements in the additional template parameter and neither
would express all restrictions. To put container subject to bed, if we believe
a collection is a container, we could just use both mine and yours parameters
and by that become more accurate but still inexact as containers have other
requirements. I have never intended to express all restrictions though, just to
solve the issue.
What is more interesting to me, however, is the vague definition of "collection"
in the OP post. Seems both of us have read that as "container". I am now having
a second thought because a better (more general and suitable to OP code)
definition for "collection" would be a range-expression usable in range-based
"for" statement -- because it is its only property that the OP code actually
depends on (other than being able to insert the element to stream; but that the
requirement on element rather than collection type).
Unfortunately, neither mine nor your code works on something like "int a[] =
{5,6,7};"
I actually tried to get it working by changing the second template parameter to
"typename T = decltype(*std::begin(Collection()))"; that works for list and
vector but produces wrong result for int[] (due to automatic conversion to int*,
I believe). I may return to this later unless solved by you or someone else. The
current plan is to define some "range_expression_traits" class template that
would cover all cases of range expressions usable in range-based "for" (again, I
don't see it useful to try to cover all restrictions but rather to make all
interesting usages work although in this case the restrictions are minimal so
they might as well get all covered automatically). I wonder though if some
for-range-expression check or trait already exists so I will be re-inventing the
wheel.
On a side note, I would not want to require client code specify wrapper or
otherwise be anyhow changed; to me the challenge is to make the code that
seemingly should work out-of-box work out-of-box if at all possible.
-Pavel