Home Projects Jobs Clientele Contact


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

Re: New public API

I am aware of retval optimization, though I am not sure about semantics in case of C++ - what if copy constructor has side effects?

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

-----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

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
[3] http://www.boost.org/libs/smart_ptr/shared_ptr.htm

Authoright © Total Knowledge: 2001-2008