By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,419 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,419 IT Pros & Developers. It's quick & easy.

Optimisation, function returns, STL containers

P: n/a
What optimisation do compilers typically provide when passing STL
containers around?

For example, if I do something like this:

struct Tbloggs
{
std::string s1;
};
typedef std::vector<Tbloggs> TbloggsList;
typedef std::vector<TbloggsList> TbloggsListContainer;

TbloggsList getBloggsList(void)
{
TbloggsList tempBloggsList;
Tbloggs b1;
tempBloggsList.push_back(b1);
return(tempBloggsList);
}

TbloggsListContainer myBloggsListContainer;
TbloggsList myBloggsList = getBloggsList();
myBloggsListContainer.push_back(myBloggsList);

In theory there's a whole lot of unnecessary copying going on here, in
the "push_back"s and the getBloggsList() return. How much of this is the
compiler allowed to optimise away?

--
Simon Elliott
http://www.ctsn.co.uk/


Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Simon Elliott" <si***@nospam.demon.co.uk> wrote...
What optimisation do compilers typically provide when passing STL
containers around?
The same as for any other classes. That's the whole point.
For example, if I do something like this:

struct Tbloggs
{
std::string s1;
Supposedly 'std::string' has been defined here...
};
typedef std::vector<Tbloggs> TbloggsList;
typedef std::vector<TbloggsList> TbloggsListContainer;

TbloggsList getBloggsList(void)
{
TbloggsList tempBloggsList;
Tbloggs b1;
tempBloggsList.push_back(b1);
return(tempBloggsList);
}

TbloggsListContainer myBloggsListContainer;
TbloggsList myBloggsList = getBloggsList();
myBloggsListContainer.push_back(myBloggsList);

In theory there's a whole lot of unnecessary copying going on here, in
the "push_back"s and the getBloggsList() return. How much of this is the
compiler allowed to optimise away?


The construction of 'myBloggList' object is permitted to happen
"in place" (see 12.8/15). It's called "return value optimisation".

Victor
Jul 19 '05 #2

P: n/a
Victor Bazarov wrote:
"Simon Elliott" <si***@nospam.demon.co.uk> wrote...
What optimisation do compilers typically provide when passing STL
containers around?



In theory there's a whole lot of unnecessary copying going on here, in
the "push_back"s and the getBloggsList() return. How much of this is the
compiler allowed to optimise away?

The construction of 'myBloggList' object is permitted to happen
"in place" (see 12.8/15). It's called "return value optimisation".


Though it won't happen in assignments, so you'll need to be
carefull of any other statements like:

myBloggsList = getBloggsList();

which isn't a construction. But you should be aware of this
with any object that is returned by value.

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.