[odb-users] std::transform changes the begin iterator

Boris Kolpackov boris at codesynthesis.com
Tue Oct 27 08:50:20 EDT 2015


Hi,

Zhao Xiang <xiang.zhao at gamegou.com> writes:

> odb::result<user_player> players = db.query<user_player>(...);
> 
> std::vector<uint32_t> playerIds;
> playerIds.resize(players.size());
> std::transform(players.begin(), players.end(), playerIds.begin(), ...);
> 
> assert(players.size() > 0);//ok
> assert(players.begin() != players.end());//oops
> 
> After some debug I noticed the value of players.begin() is changed
> after the std::transform call.

>From Section 4.4, "Query Result":

"It is best to view an instance of odb::result as a handle to a stream, such
as a socket stream. While we can make a copy of a result or assign one result
to another, the two instances will refer to the same result stream. Advancing
the current position in one instance will also advance it in another."

And:

"The result iterator is an input iterator which means that the only two
position operations that it supports are to move to the next object and to
determine whether the end of the result stream has been reached. In fact, the
result iterator can only be in two states: the current position and the end
position. If we have two iterators pointing to the current position and then
we advance one of them, the other will advance as well."

Which means that after you have iterated over the result, (in transform()),
you cannot re-iterate over it without re-executing the query.

Boris



More information about the odb-users mailing list