C++ iterators considered harmful?

后端 未结 13 648
忘了有多久
忘了有多久 2020-12-23 03:42

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, s

13条回答
  •  醉酒成梦
    2020-12-23 03:43

    Isn't Andrei trying to do some hidden marketing for the D language (currently he is working with it)...?

    Andrei states that containers are ok, but iterators are ugly, non-intuitive, error-prone and dangerous, hard to implement (well this last one seems to be rather true...) And what do we have in C++... pointers? Aren't they ugly/.../dangerous? But we happily embraced them and live with them.

    Which one is more intuitive to write:

    for(auto i=foo.begin();i!=foo.end();++i)
        bar(*i);
    

    or

    for (auto r=foo.all(); !foo.empty(); foo.popFront())
            bar(r.front());
    

    Iterators concept can be complemented with ranges and other ideas, but I think that they have their place and won't be replaced.

提交回复
热议问题