A
Andrew Ward
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.
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.