简体   繁体   English

返回类型为“ input_iterator :: operator *”?

[英]return type for `input_iterator::operator*`?

I'm doing an input iterator wrapper for a QSqlQuery (for traversing rows and constructing a new class type object for each row), and I want to know what must be the returned type of operator* . 我正在为QSqlQuery做一个输入迭代器包装器(用于遍历行并为每行构造一个新的类类型对象),我想知道operator*的返回类型必须是什么。

According to cppreference in the input iterator concept description , the returned type of *i must be: 根据输入迭代器概念描述中的cppreference ,返回的*i类型必须为:

reference, convertible to value_type 参考,可转换为value_type

and curiously, I'm in doubt about if that comma , represent an or or and and ("reference to value_type or/and convertible to value_type"), because in my case, I construct a new object (whose type is a class template parameter) when calling operator* . 和奇怪的是,我在怀疑如果逗号,代表or或和and (“参考VALUE_TYPE和/或转换为VALUE_TYPE”),因为在我的情况,我建立一个新的对象(其类型是一个类模板当调用参数) operator* Consequently, I cannot return a reference. 因此,我无法返回参考。

To match the input iterator "concept", can I return the object by value? 为了匹配输入迭代器“ concept”,我可以按值返回对象吗?

iterator_traits<> must define 5 types, one of which is reference , hence the comma. iterator_traits<>必须定义5种类型,其中之一是reference ,因此是逗号。 You can return by value; 您可以按价值返回; it's only fair to do so for const_iterator , thought: for non-const, you cannot modify the original. 对于const_iterator这样做是合理的,以为:对于非const,您不能修改原始的。 I guess this is ok here as you have an sql query. 我想这是可以的,因为您有一个SQL查询。 In this case you do typedef value_type reference; 在这种情况下,您可以使用typedef value_type reference; inside iterator_traits<T> . iterator_traits<T>内部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM