Is it defined to provide an inverted range to C++ standard algorithms?

后端 未结 5 597
南旧
南旧 2021-01-17 19:29

Consider standard algorithms like, say, std::for_each.

template
Function for_each(InputIterator first         


        
5条回答
  •  执笔经年
    2021-01-17 19:54

    Does that mean that the following is technically valid? Or is it undefined? What can I realistically expect it to do?

    No it is not. Your code would exhibit undefined behavior when for_each increments the iterator and that iterator would be pointing to end and there is nothing to dereference(Well, it is enough to get undefined behavior at this point, so there is no point talking about past end)!

提交回复
热议问题