Why does the upcoming Ranges library not support container initialization from a range?

女生的网名这么多〃 提交于 2020-01-04 04:56:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!