为什么需要 C++ 迭代器来返回引用? [英] Why are C++ iterators required to return a reference?

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

问题描述

我正在实现一个迭代器,它对生成器函数的结果进行迭代,而不是对内存中的数据结构(例如向量或映射)进行迭代.

I'm implementing an iterator that iterates over the results of a generator function rather than over a data structure in memory such as a vector or map.

通读 C 的最终工作草案++17 §27.2.3,输入迭代器(以及大多数其他迭代器)的解引用运算符的返回类型前向迭代器 需要作为引用.这对于迭代器正在迭代的数据结构中存在的项目很好.但是,因为我没有使用数据结构,而是在调用取消引用运算符时计算每个项目,所以我没有要返回的有效引用;当操作员返回时,计算项被销毁.为了解决这个问题,我将计算结果存储在迭代器本身中,并返回对存储结果的引用.这适用于我的用例,但在与任意用​​户定义类型一起使用时有其自身的问题.

Reading through the final working draft for C++17 §27.2.3, the return type of the dereference operator for an input iterator (and by extension, most other iterators) a forward iterator is required to be a reference. This is fine for items that exist in the data structure the iterator is iterating over. However, because I'm not using a data structure and am calculating each item when the dereference operator is called, I don't have a valid reference to return; the calculated item is destroyed when the operator returns. To work around this, I am storing the result of the calculation in the iterator itself and returning a reference to the stored result. This works fine for my use case, but has issues of its own when used with arbitrary user-defined types.

我可以理解迭代器被允许返回一个引用,但为什么这会成为非变异迭代器的要求?标准的作者是否不认为生成器和动态转换是迭代器的有效用例?返回一个值而不是一个 const 引用会造成任何实际伤害吗?

I can understand iterators being allowed to return a reference, but why would this be a requirement for non-mutating iterators? Did the writers of the standard not consider generators and on-the-fly transformations to be valid use cases for iterators? Would returning a value instead of a const reference cause any actual harm?

:出于对为什么标准如此编写的好奇,我问了更多问题,因为我已经有了一个非常好的解决方法.

[edit]: I'm asking more out of curiosity about why the standard is written the way it is, since I already have a perfectly good workaround.

推荐答案

取消引用输入迭代器不需要产生泛左值(即返回引用).输入迭代器要求说解引用时的返回类型必须是reference, convertible to T"但没有任何地方说reference必须是引用类型.

Dereferencing an input iterator is not required to yield a glvalue (that is, return a reference). The input iterator requirements say that the return type when dereferencing must be "reference, convertible to T" but nowhere does it say that reference must be a reference type.

然而,需要取消引用前向迭代器才能产生泛左值:

However, dereferencing a forward iterator is required to yield a glvalue:

如果X 是可变迭代器,reference 是对T 的引用;如果 X 是一个常量迭代器,reference 是对 const T 的引用,

if X is a mutable iterator, reference is a reference to T; if X is a constant iterator, reference is a reference to const T,

所以继续编写您的迭代器来动态生成元素,但它只能是输入迭代器,而不是前向迭代器.对于许多算法来说,这已经足够了(例如 std::for_eachstd::all_of).

So go ahead and write your iterator that generates elements on the fly, but it can only be an input iterator, not a forward iterator. For many algorithms this is sufficient (e.g., std::for_each and std::all_of).

这篇关于为什么需要 C++ 迭代器来返回引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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