C++ 迭代器被认为是有害的? [英] C++ iterators considered harmful?

查看:23
本文介绍了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.

  1. 迭代器不好吗?
  2. 他的替代者真的更好吗?
  3. C++ 实现者会采纳他的想法吗?

推荐答案

首先回答你的问题:

  1. 没有.事实上,我在其他地方认为迭代器是最重要/基本的概念计算机科学.我(不像安德烈)也认为迭代器是直观的.
  2. 是的,当然可以,但这不足为奇.
  3. 嗯.查看 Boost.Range 和 C++0x– 他们不是已经了吗?
  1. 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.
  2. Yes, definitely but that shouldn't come as a surprise.
  3. 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++ 迭代器被认为是有害的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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