[C++-sig] copy_const_reference or return_internal_reference?

David Abrahams dave at boost-consulting.com
Wed Sep 25 03:02:27 CEST 2002

From: "Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com>

> I have an object composed of 12 double. A const& to this object is
returned by
> a member function of another class. From the viewpoint of using the
> object in Python I do not care if I get a copy or a reference to the
> object. In Boost.Python Version 2 I have the choice of using
> copy_const_reference or return_internal_reference. Are there
> that would lead me to prefer one over the other, such as size of
generated code
> or memory overhead?

copy_const_reference will make an instance with storage for one of your
objects, size = base_size + 12 * sizeof(double). return_internal_reference
will make an instance with storage for a pointer to one of your objects,
size = base_size + sizeof(void*). However, it will also create a weak
reference object which goes in the source object's weakreflist and a
special callback object to manage the lifetime of the internally-referenced
object. My guess? copy_const_reference is your friend here, resulting in
less overall memory use and less fragmentation, also probably fewer total

           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com

More information about the Cplusplus-sig mailing list