Why do iterators need to be default-constructible

后端 未结 3 1118
忘掉有多难
忘掉有多难 2020-12-30 20:09

Iterators of the categories forward, bidirectional, and random access need to be default-constructible.

Why is this, and why do input

3条回答
  •  [愿得一人]
    2020-12-30 20:50

    Forward / bidirectional / random access iterators can often be pointers - it would historically have aided migration from pointer-using code to iterators if the construction and initialisation could be left delocalised if that was the way the code happened to be. Forcing more wholesale change would have frustrated a lot of people trying to migrate older code off explicit use of pointers and onto iterators. Changing it would now break a lot of code.

    Input and output operators are often most elegantly implemented with references to underlying stream or other I/O objects, and references must be initialised at construction. Of course, implementations could be forced to defer that, and use pointers internally, but that's sure to rub some people up the wrong way - seeming too "C"-like - so it's unsurprising the Standard facilitates use of references.

提交回复
热议问题