C++ iterators considered harmful?(C++ 迭代器被认为是有害的?)
问题描述
在今天的 Boost 库会议上,Andrei Alexandrescu,《Modern C++ Design andLoki C++ 库,发表了题为Iterators Must Go"的演讲(视频,slides) 关于为什么迭代器不好,他有一个更好的解决方案.
At the Boost library conference today, Andrei Alexandrescu, author of the book Modern C++ Design and the Loki C++ library, gave a talk titled "Iterators Must Go" (video, slides) about why iterators are bad, and he had a better solution.
我尝试阅读演示幻灯片,但无法从中获得太多信息.
I tried to read the presentation slides, but I could not get much out of them.
- 迭代器不好吗?
- 他的替代者真的更好吗?
- C++ 实现者会采纳他的想法吗?
推荐答案
首先回答你的问题:
- 没有.事实上,我在其他地方认为迭代器是最重要/基本的概念计算机科学.我(不像安德烈)也认为迭代器是直观的.
- 是的,当然可以,但这不足为奇.
- 嗯.查看 Boost.Range 和 C++0x– 他们不是已经了吗?
- No. In fact, I argued elsewhere that iterators are the most important/fundamental concept of computer science ever. I (unlike Andrei) also think that iterators are intuitive.
- Yes, definitely but that shouldn't come as a surprise.
- Hmm. Looking at Boost.Range and C++0x – haven't they already?
Andrei 在这里所做的重大贡献只是说:完全放弃迭代器的概念,将范围不仅仅是一个方便的包装器,而是一个核心构造.其他语言已经做到了这一点(Andrei 的大部分概念只是呼应 .NET 的 LINQ 或 Python 的迭代器),但它们都只提供输出范围.Andrei 主张不同类型的范围,就像传统的迭代器类别一样.
Andrei's big contribution here is just to say: drop the concept of iterators altogether, see ranges not just as a convenience wrapper but rather as a core construct. Other languages have already done this (much of Andrei's concepts just echo .NET's LINQ or Python's iterators) but they all only offer output ranges. Andrei argues for different types of ranges, much like the conventional iterator categories.
从这个角度来看,他开始嘲笑这些迭代器类别的任意性是很奇怪的.
In that light, it's odd that he starts by mocking the arbitrariness of these iterator categories.
我还认为他的示例已经过时,尤其是他的文件复制:是的,迭代器变体是对 1975 年代码的巨大改进.它将具有复杂中断条件的循环简化为 one 语句.他在这里真正遇到的问题只是语法.好吧,对不起:我们在这里谈论的是 C++——当然语法很丑.是的,在这里使用范围是一种改进——但只是在语法上.
I also think that his examples are off, especially his file copying: yes, the iterator variant is a huge improvement over the 1975 code. It reduces a loop with complicated break condition down to one statement. What he's really taking issue with here is just the syntax. Well, excuse me: we're talking about C++ here – of course the syntax is ugly. And yes, using ranges here is an improvement – but only syntactically.
我还认为 Andrei 的 find
实现已关闭.他真正定义的是来自 LINQ 的 DropUntil
操作(命名很难!).find
操作实际上应该返回一个或零个元素(或迭代器!).在我看来,在这里避开迭代器没有帮助,因为我们可能想直接修改值而不是复制它.在这里返回一个元素范围只会增加开销而没有好处.以 Andrei 的方式做是不好的,因为方法的名称是错误的和误导性的.
I also think that Andrei's find
implementation is off. What he really defines there is the DropUntil
operation (naming is hard!) from LINQ. The find
operation should really return either one or zero elements (or an iterator!). Shunning iterators here isn't helpful in my opinion since we might want to modify the value directly instead of copying it. Returning a one-element range here only adds overhead without a benefit. Doing it Andrei's way is bad because then the name of the method is just wrong and misleading.
也就是说,我几乎在所有方面都同意 Andrei.迭代器虽然是我从计算机科学中获得的宠物概念,但肯定是一个很大的语法负担,许多范围(尤其是无限生成器)可以(并且应该)在没有它们的情况下方便地实现.
That said, I essentially agree with Andrei in almost all points. Iterators, while being my pet concept from computer science, are certainly a big syntactical burden and many ranges (especially infinite generators) can (and should) be implemented conveniently without them.
这篇关于C++ 迭代器被认为是有害的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ 迭代器被认为是有害的?
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01