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

查看:179
本文介绍了C ++迭代器认为有害?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Boost图书馆大会上, Andrei Alexandrescu ,现代C + +设计一书Loki C ++库发表了题为Iterators Must Go的演讲(视频 slides )关于为什么迭代器是坏的,他有一个更好的解决方案。



我试图阅读演示文稿幻灯片,但我不能从中学到很多东西。





  1. >

    解决方案

    首先,回答您的问题:


    1. 否。事实上,我认为其他地方的迭代器是最重要的/基本的概念计算机科学。 I(与Andrei不同)也认为迭代器是直观的。

    2. 是的,但这不应该是一个惊喜。

    3. Hmm。查看 Boost.Range 和C ++ 0x - haven他们已经是?

    Andrei在这里的重要贡献只是说:放弃迭代器的概念,方便包装而是作为核心结构。其他语言已经做到了(大部分的Andrei的概念只是回应.NET的LINQ或Python的迭代器),但他们只提供输出范围。 Andrei认为不同类型的范围,很像传统的迭代器类别。



    在这种情况下,奇怪的是他开始嘲笑这些迭代器类别的任意性。我也认为他的例子,特别是他的文件复制:是的,迭代器变体是一个巨大的改进比1975年的代码。它将具有复杂中断条件的循环减少到 one 语句。他真正采取的问题在这里只是语法。好吧,对不起,我们在这里谈论C ++ - 当然的语法是丑陋的。



    我也认为Andrei的 find 是一个改进, code>实现关闭。他真正定义的是来自LINQ的 DropUntil 操作(命名很难! find 操作应该返回一个或零个元素(或一个迭代器!)。在这里忽略迭代器在我看来是没有帮助的,因为我们可能想要直接修改值,而不是复制它。在这里返回一个元素范围只增加了开销而没有好处。做它安德烈的方式是坏的,因为那个方法的名称是错误的和误导。



    这说,我基本上同意Andrei几乎所有的点。迭代器虽然是我计算机科学的宠物概念,但是它确实是一个很大的语法负担,许多范围(特别是无限生成器)可以(而且应该)没有它们就能方便地实现。


    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. Are iterators bad?
    2. Is his replacement really better?
    3. Will C++ implementators pick up his ideas?

    解决方案

    First, to answer your questions:

    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'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.

    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.

    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.

    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天全站免登陆