Mailinglist Archive: zypp-devel (149 mails)

< Previous Next >
[zypp-devel] ZyppHacker: Please use the correct/reasonable insert iterator!
  • From: Michael Andres <ma@xxxxxxx>
  • Date: Mon, 22 Sep 2008 13:52:59 +0200
  • Message-id: <20080922115259.GA18002@xxxxxxx>
Hi!


std::vector<RepoInfo> toModify;
getRepositoriesInService(oldAlias,
std::insert_iterator<std::vector<RepoInfo> >( toModify, toModify.begin()
));


Reading such a line you know why some people complain about templates.

a) It's not readable.
b) It's not reasonable.



Usually there are also convenience methods that build the right
object for you. For the purpose here STL provides:

std::back_inserter(container&)
std::front_inserter(container&)


Now the above can be written as:

std::vector<RepoInfo> toModify;
getRepositoriesInService( oldAlias, std::front_inserter(toModify) );



The benefit is IMO obvious. And now as we can read the code, we
may recognize that we don't want to use a front_inserter with a
vector. We don't want to shift the whole array to the right on each
insert. We want this:

getRepositoriesInService( oldAlias, std::back_inserter(toModify) );



or
getRepositoriesInService(oldAlias,
std::insert_iterator<std::vector<RepoInfo> >( toModify, toModify.end() ));

Whenever you have to write such a beast, you should ask yourself whether
it's not worth writing a short convenience method. You already wrote the
body:

insert_iterator<std::vector<RepoInfo> >( toModify, toModify.end() )


Just put it into a function with a resonable name:

template<class Container>
inline insert_iterator<Container> back_inserter( Container & c )
{ return insert_iterator<Container>( c, c.end() ); }

--

cu,
Michael Andres

+------------------------------------------------------------------+
Key fingerprint = 2DFA 5D73 18B1 E7EF A862 27AC 3FB8 9E3A 27C6 B0E4
+------------------------------------------------------------------+
Michael Andres YaST Development ma@xxxxxxxxxx
SUSE LINUX Products GmbH, GF: Markus Rex, HRB 16746 (AG Nuernberg)
Maxfeldstrasse 5, D-90409 Nuernberg, Germany, ++49 (0)911 - 740 53-0
+------------------------------------------------------------------+

--
To unsubscribe, e-mail: zypp-devel+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: zypp-devel+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages