473,411 Members | 2,230 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Returning containers

Hi,

A third-party container library I use implements its containers using
copy-on-write semantics, making it efficient to return an instance of
the container by value from a function. For example:

ThirdPartyContainer<MyClass> foo();

If I want to refactor this function to use std::list I could do this:

std::list<MyClass> foo();

Which as far as I know could be either efficient or inefficient
depending on the library implementation.

What I wanted to know is what technique other people use? I can think of
a few other ways of writing the function:

void foo(std::list<MyClass> * toFill);
or
void foo(std::list<MyClass> & toFill);
or
std::auto_ptr<std::list<MyClass> > foo();

None of which are as easy to use as the return by value approach.
Nov 28 '05 #1
3 1993
Andrew Ward wrote:
Hi,

A third-party container library I use implements its containers using
copy-on-write semantics, making it efficient to return an instance of
the container by value from a function. For example:

ThirdPartyContainer<MyClass> foo();

If I want to refactor this function to use std::list I could do this:

std::list<MyClass> foo();

Which as far as I know could be either efficient or inefficient
depending on the library implementation.
And depending on the implementation of the C++ compiler itself. A good one
can optimize the copy away.
What I wanted to know is what technique other people use? I can think of
a few other ways of writing the function:

void foo(std::list<MyClass> * toFill);
or
void foo(std::list<MyClass> & toFill);
or
std::auto_ptr<std::list<MyClass> > foo();

None of which are as easy to use as the return by value approach.


The typical way used by the C++ standard library is to work with iterators
instead of containers.

Nov 28 '05 #2
Rolf Magnus wrote:
Andrew Ward wrote:

Hi,

A third-party container library I use implements its containers using
copy-on-write semantics, making it efficient to return an instance of
the container by value from a function. For example:

ThirdPartyContainer<MyClass> foo();

If I want to refactor this function to use std::list I could do this:

std::list<MyClass> foo();

Which as far as I know could be either efficient or inefficient
depending on the library implementation.

And depending on the implementation of the C++ compiler itself. A good one
can optimize the copy away.

What I wanted to know is what technique other people use? I can think of
a few other ways of writing the function:

void foo(std::list<MyClass> * toFill);
or
void foo(std::list<MyClass> & toFill);
or
std::auto_ptr<std::list<MyClass> > foo();

None of which are as easy to use as the return by value approach.

The typical way used by the C++ standard library is to work with iterators
instead of containers.


That's fine if the collection is a member of some object, then I could
provide member functions:
std::list<MyClass>::const_iterator begin() const;
std::list<MyClass>::const_iterator end() const;

However quite often the called function will be generating the
collection data.

For example:
MyPeopleDatabase d;
std::list<People> allPeople() const;

How could that be done with iterators? I cannot do:
std::pair<std::list<People>::const_iterator,
std::list<People>::const_iterator> allPeople() const;

As there is no where I want to persist the collection inside 'd'.

Nov 28 '05 #3

"Andrew Ward" <an*******@ihug.co.nz> wrote in message
news:2t*******************@news.xtra.co.nz...
Hi,

A third-party container library I use implements its containers using
copy-on-write semantics, making it efficient to return an instance of the
container by value from a function. For example:

ThirdPartyContainer<MyClass> foo();

If I want to refactor this function to use std::list I could do this:

std::list<MyClass> foo();

Which as far as I know could be either efficient or inefficient depending
on the library implementation.

What I wanted to know is what technique other people use? I can think of a
few other ways of writing the function:

void foo(std::list<MyClass> * toFill);
or
void foo(std::list<MyClass> & toFill);
or
std::auto_ptr<std::list<MyClass> > foo();

None of which are as easy to use as the return by value approach.


I prefer the approach of passing a reference. This is the way I often
handle such problems, regardless of what kind of container or object it is
I'm talking about. Even plain C-style arrays are often best handled like
this (assuming you're stuck using them in the first place), where you pass
the function a pointer to the first element (and a count of the elements),
and let them go to town on it.

I think of it like this: I'm giving someone a blank form which they need to
fill out. This leaves me in complete charge of the lifetime of the "form",
from its construction through its destruction, and all I ask of others
(i.e., functions), is that they use the form I give them, whether that's
filling it out, editing it, or building some kind of report from it.

This approach means I never get confused about who "owns" a dynamically
allocated object, and cleanup is much easier to handle.

-Howard

Nov 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Sachin Garg | last post by:
Hi, When trying to return objects of type std::list<MyClass> from my function, I get a corrupt heap. (I checked using MSVC++ 7.0 runtime heap check facility) Basically, I am creating a...
19
by: Steven T. Hatton | last post by:
I believe it is technically possible to return a pointer to the first element of an array. I can persuade the returned pointer to act like an array, with some qualifications. I'm specifically...
14
by: phil_gg04 | last post by:
Dear C++ Experts, Over the last couple of months I have been writing my first program using shared memory. It has been something of an "in-at-the-deep-end" experience, to say the least. At...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
1
by: Jean-Marc Blaise | last post by:
IBM recommends to place each ts container on a different physical disk. What about the impacts if all containers (DMS) are in the same FS (supported by multiple disks) ? Does DB2 preallocate...
26
by: cdg | last post by:
Could anyone correct any mistakes in this example program. I am just trying to return an array back to "main" to be printed out. And I am not sure how a "pointer to an array" is returned to the...
13
by: jois.de.vivre | last post by:
Hi All, I'm trying to write a wrapper class for std::vector to extend some of its functionality. The problem I'm getting into is returning an iterator type from a member function. Here is the...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
6
by: RThaden | last post by:
Hi together, I am a bit clueless about the following problem: In the following code excerpt std::string getStr() { std::string bla="asdfsa";
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.