Mailinglist Archive: zypp-devel (68 mails)

< Previous Next >
Re: [zypp-devel] Things TODO in CacheStore
  • From: Michael Andres <ma@xxxxxxx>
  • Date: Fri, 25 May 2007 14:21:48 +0200
  • Message-id: <20070525122148.GA8497@xxxxxxx>
On Fri, May 25, Jan Kupec wrote:

> > by "value" or by "const &".
> 
> What's better here? Passing the pointer by value or reference? I used
> const & to declare constness (shell we possibly modify the object?) and
> to avoid copying.

I'm a fan of "const &", unless it is an integral type.

No matter how cheap it is to copy class, it's hardly cheaper than a 
"const &".

Even a class like a smart pointer (or any other class using PIMPL, a
std::string e.g. ) has to adjust at least a refcount (2times+1check for 
counter==0), if a copy is created.



And be aware that a copy of a container like list/set/map is not cheap! 
It is right, that the copies share the payload data, but the list/tree 
structure is duplicated. 

That's one reason why one should consider providing iterators, instead of 
passing back containers.

  class Foo {
NOT:
  std::list<int> getlist() const;
OR:
  const std::list<int> & getlist() const;

BUT:
  typedef std::list<int>                ContainerType;
  typedef ContainerType::size_type      size_type;
  typedef ContainerType::value_type     value_type;
  typedef ContainerType::iterator       iterator;
  typedef ContainerType::const_iterator const_iterator;


  bool empty()           const          { return container.empty(); }
  size_type size()       const          { return container.size(); }       
  const_iterator begin() const          { return container.begin(); }
  const_iterator end()   const          { return container.end(); }

  // in most classes you will NOT provide nonconst iterators!
  iterator begin()                      { return container.begin(); }
  iterator end()                        { return container.end(); }
  };


One benefit for the user: He's not tied to a certain container:

   Foo foo;

   std::list<int>   mLists( foo.begin(), foo.end() ); 
   std::set<int>    mSet  ( foo.begin(), foo.end() );
   std::vector<int> mVec  ( foo.begin(), foo.end() );

   // or

   std::for_each( foo.begin(), foo.end(), &doSomething );






-- 

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 >