CPPSERV


Home Projects Jobs Clientele Contact

cppserv


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: New public API



On Mon, 2006-02-20 at 22:37 +0000, Ilya Volynets wrote:
> I am aware of retval optimization, though I am not sure about semantics in case of C++ - what if copy constructor has side effects?
> 
Are you sure there's a guarantee that a copy constructor will be called
when an object is returned by value? I think there's no such guarantee,
nor there should be any.

> As for optimizations on copy from
> The library, I'm not sure again - I
> think it's *supposed* to copy in this case, but I don't remember.
> 
> Auto-ptr/ref counting is something I'd prefer, and it's semantically closest to java

Now the question is: is allocating heap memory via new is that much
faster then copying an object? (Though in case of a big vector is sure
is)

> 
> -----Original Message-----
> From: Krzysztof Rzymkowski <rzymek@gmail.com>
> Date: Mon, 20 Feb 2006 23:15:18 
> To:C++ Servlet Engine Discussion <cppserv@total-knowledge.com>
> Subject: Re: New public API
> 
> On Mon, 2006-02-20 at 11:00 -0800, Ilya A. Volynets-Evenbakh wrote:
> > One thing I was thinking about (and even started using in some cases)
> > is to change methods returing collections. The way it is now:
> > virtual std::vector<std::string>  getAttributeNames()
> > This means that whole vector, as well as every string inside will be copied
> > (possibly twice - inside function when creating retval, and then to the
> > actual
> > return value). Now, instead we could change signiture as follows:
> > virtual void getAttributeNames(std::vector<std::string> &out)
> > This will avoid unnessesary copying.
> > 
> > Java does not have to worry about it, since it returns references, and thus
> > only one copy of object is created.
> 
> It is possible for a STL implementation to user reference counting in
> vector's copy constructor, thought the SGI's STL does simple copying.
> It is as well possible for the compiler to use return value optimization
> [1], thought it is not always possible to write the getters in a way
> which allows RVO. 
> 
> Another option would be to use std::auto_ptr [2] or boost::shared_ptr
> [3]. 
> 
> I would like to keep the API as close to the Java version as it is
> possible. So I'd rather stick with option 1 (ignore the problem) or 2
> user some sort of reference counting.
> 
> [1] http://cpptips.hyperformix.com/cpptips/ret_val_opt
> [2] http://gcc.gnu.org/onlinedocs/libstdc
> ++/latest-doxygen/classstd_1_1auto__ptr.html
> [3] http://www.boost.org/libs/smart_ptr/shared_ptr.htm
-- 


Authoright © Total Knowledge: 2001-2008