简体   繁体   English

为什么 C++ 20 范围算法在<algorithm>和意见<ranges> ?

[英]Why are C++ 20 range algorithms in <algorithm> and views in <ranges>?

I admit this is nitpicking about naming but beside my nitpicking about naming I have a bit of a real life code review experience where I have seen people include <ranges> when they only need <algorithm> eg when using std::ranges::count_if .我承认这是对命名的挑剔,但除了我对命名的挑剔之外,我还有一些现实生活中的代码审查经验,我看到人们在只需要<algorithm>时包含了<ranges> ,例如在使用std::ranges::count_if .

So why put range algorithms in <algorithm> and views in <ranges> ?那么为什么将范围算法放在<algorithm>而将视图放在<ranges> It would make more sense to me to either:对我来说更有意义的是:

  • put range algorithms in <algorithm> and rename <ranges> to <views> / <range_views>将范围算法放入<algorithm>并将<ranges>重命名为<views> / <range_views>
  • put both range algorithm and views in <ranges>将范围算法和视图都放在<ranges>

note: I know I am oversimplifying the distinction between headers, <ranges> also contains a lot of concepts and building blocks , so it is possible that is part of the justification.注意:我知道我过于简化了标题之间的区别, <ranges>还包含很多概念和构建块,因此这可能是理由的一部分。

I am not the author of the proposals, so I cannot canonically answer why the choice was made.我不是提案的作者,所以我无法规范地回答做出这个选择的原因。 At best, I can suggest reasons why the choice makes sense.充其量,我可以提出选择有意义的原因。

The One Ranges Proposal P0896R4 doesn't seem to have rationale for the choice. One Ranges 提案 P0896R4 似乎没有选择的理由。 Range Adaptors and Utilities P0789R3 did not have these algorithms. Range Adapters and Utilities P0789R3 没有这些算法。

So why put range algorithms in <algorithm>那么为什么要把范围算法放在<algorithm>

The clue is in the name :) They are algorithms, and algorithms can be found in this header.线索在名称中 :) 它们是算法,可以在此标题中找到算法。

If someone is looking for find_if , they can find both the iterator algorithm and the range algorithm in the same place.如果有人正在寻找find_if ,他们可以在同一个地方找到迭代器算法和范围算法。

So why put range [...] views in <ranges> ?那么为什么要把 range [...] 视图放在<ranges>呢?

There used to not be views in any of the existing headers, so using the existing ones might not have been appropriate.过去在任何现有标题中都没有视图,因此使用现有标题可能并不合适。 Hence a new header was introduced.因此引入了一个新的标题。

There was an analogue for subset of views, namely range adaptor views.有一个类似的视图子集,即范围适配器视图。 There were (and still are) iterator adaptors.曾经(现在仍然是)迭代器适配器。 Those are defined in <iterator> .这些在<iterator>中定义。 I don't think that would have been a good place to put range adaptors.我认为那不是放置范围适配器的好地方。

put range algorithms in <algorithm> and rename to <views> / <range_views>将范围算法放入<algorithm>并重命名为<views> / <range_views>

But <ranges> contains more than just views.但是<ranges>不仅仅包含视图。 It contains concepts such as ranges::range , range primitive classes such as ranges::sentinel_t , range specific iterators such as ranges::borrowed_iterator_t , functions that are analogous to iterator facilities such as std::ranges::begin .它包含的概念,如ranges::range ,范围原始类,如ranges::sentinel_t ,特定范围内的迭代器,如ranges::borrowed_iterator_t ,即类似于迭代设施诸如函数std::ranges::begin Naming the header <views> might have been misleading.命名标题<views>可能会产生误导。

put both range algorithm and views in <ranges>将范围算法和视图都放在<ranges>

Perhaps if the iterator algorithms had originally been placed in <iterator> , then range algorithms would have been placed in <ranges> to follow that convention.也许如果迭代器算法最初被放置在<iterator> ,那么范围算法将被放置在<ranges>以遵循该约定。 But as it is, the previous convention was to split the algorithms into a separate header, and that convention was followed with ranges.但实际上,之前的约定是将算法拆分为单独的标题,并且遵循该约定的是范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM