What's wrong with passing C++ iterator by reference?

前端 未结 7 654
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 05:13

I\'ve written a few functions with a prototype like this:

template 
int parse_integer(input_iterator &begin, input_iterato         


        
7条回答
  •  长情又很酷
    2020-12-24 06:10

    When they say "don't pass by reference" maybe that's because it's more normal/idiomatic to pass iterators as value parameters, instead of passing them by const reference: which you did, for the second parameter.

    In this example however you need to return two values: the parsed int value, and, the new/modified iterator value; and given that a function can't have two return codes, coding one of the return codes as a non-const reference is IMO normal.

    An alternative would be to code it something like this:

    //Comment: the return code is a pair of values, i.e. the parsed int and etc ...
    pair parse(input_iterator start, input_iterator end)
    {
    }
    

提交回复
热议问题