为什么C ++ 20范围库具有自己的名称空间? [英] Why does the C++20 range library have its own namespace?

查看:56
本文介绍了为什么C ++ 20范围库具有自己的名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在 range 名称空间中实现 std :: range :: sort (以及其他基于范围的算法)?为什么不将它定义为 std :: sort 的重载并取一个范围?

Why is std::range::sort (and other range-based algorithms) implemented in the range namespace? Why isn't it defined as an overload of std::sort taking a range?

推荐答案

这是为了避免破坏现有的代码库.Eric Niebler,Sean Parent和Andrew Sutton在他们的设计文件 D4128 中讨论了不同的方法.

It's to avoid disrupting existing code bases. Eric Niebler, Sean Parent and Andrew Sutton discussed different approaches in their design paper D4128.

3.3.6算法返回类型已更改为适应哨兵

...以类似的方式,大多数算法在获得新的返回类型时普遍支持哨兵.这是一个突破性的变化在很多情况下.在某些情况下,例如 for_each ,更改不太可能非常具有破坏性.在其他情况下,可能更是如此.仅仅接受破损显然是不可接受的.我们可以想象三种方式缓解问题:

... In similar fashion, most algorithm get new return types when they are generalized to support sentinels. This is a source-breaking change in many cases. In some cases, like for_each, the change is unlikely to be very disruptive. In other cases it may be more so. Merely accepting the breakage is clearly not acceptable. We can imagine three ways to mitigate the problem:

  1. 仅在迭代器和标记的类型不同时更改返回类型.这导致界面稍微复杂一些这可能会使用户感到困惑.它还使通用代码变得非常复杂,仅使用元程序的结果就需要元编程逻辑调用一些算法.因此,这种可能性不是在这里探索.

  1. Only change the return type when the types of the iterator and the sentinel differ. This leads to a slightly more complicated interface that may confuse users. It also greatly complicates generic code, which would need metaprogramming logic just to use the result of calling some algorithms. For this reason, this possibility is not explored here.

使算法的新返回类型隐式转换为旧的返回类型.考虑 copy ,该代码当前返回输出迭代器的结束位置.更改以适应标记,返回类型将更改为 pair< I,O> ;; 之类的东西,即一对输入和输出迭代器.代替返回一个 pair ,我们可以返回一种隐式对可转换为第二个参数.这样可以避免一些破损,但是并非所有情况.这种诡计不太可能完全消失未被注意.

Make the new return type of the algorithms implicitly convertible to the old return type. Consider copy, which currently returns the ending position of the output iterator. When changed to accommodate sentinels, the return type would be changed to something like pair<I, O>; that is, a pair of the input and output iterators. Instead of returning a pair, we could return a kind of pair that is implicitly convertible to its second argument. This avoids breakage in some, but not all, scenarios. This subterfuge is unlikely to go completely unnoticed.

在用户必须选择加入的单独命名空间中提供新的标准库.在这种情况下,直到用户都不会破坏任何代码显式地移植他们的代码.用户必须适应然后更改返回类型.类似于clang的自动升级工具现代化可以在这里大有帮助.

Deliver the new standard library in a separate namespace that users must opt into. In that case, no code is broken until the user explicitly ports their code. The user would have to accommodate the changed return types then. An automated upgrade tool similar to clang modernize can greatly help here.

我们(作者)更喜欢(3).

We, the authors, prefer (3).

最终,它对使用C ++ 20支持的编译器进行构建的现有代码库的破坏性最小.这是他们自己喜欢的方法,似乎剩下的就是历史了.

Ultimately, it was to be the least disruptive to existing code bases that move onto building using C++20 enabled compilers. It's the approach they themselves preferred, and seems like the rest is history.

这篇关于为什么C ++ 20范围库具有自己的名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆