问题
Introduction
With the upcoming Ranges library, the need to denote a range with two iterators is pretty much gone. For example, instead of
if (std::equal(begin(foo), end(foo), begin(bar), end(bar)))
we have
if (std::ranges::equal(foo, bar))
The latter is arguably superior not only because of its conciseness, but also because it prevents the common pitfall of omitting end(bar)
and welcoming bound errors.
Problem
How about the following code?
std::vector<int> vec{begin(foo), end(foo)};
where foo
is a range. With Ranges, I'd expect simplifying it into
std::vector<int> vec{foo};
However, I fail to find any mention of it in [vector] or [container.requirements]. Nor does the Ranges library introduce a new set of containers.
Why does the Ranges library not support container initialization from a range? What is the rationale?
回答1:
The goal for C++20 was to get ranges in! There were several hurdles to get over before that could happen, but once those were overcome it's quite probable the committee believed it's best to introduce a workable ranges library that is perhaps not feature complete, instead of no ranges at all.
That is not to say that this feature is undesirable, only that there were just some open questions left regarding it, but it's still in the works (see p1206).
If one reads the rationale for revision 1 of the paper, it mentions that adding constructors to the standard containers proved unworkable. Likely because the standard containers have so much retrofitted on their initialization that overload resolution becomes a nightmare.
来源:https://stackoverflow.com/questions/55950955/why-does-the-upcoming-ranges-library-not-support-container-initialization-from-a