468,101 Members | 1,338 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,101 developers. It's quick & easy.

Optimisation, function returns, STL containers

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
2 1679
"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
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.

Similar topics

3 posts views Thread by Michael Hohn | last post: by
10 posts views Thread by Ron_Adam | last post: by
17 posts views Thread by EC-AKD | last post: by
5 posts views Thread by Crirus | last post: by
26 posts views Thread by cdg | last post: by
2 posts views Thread by cremoni | last post: by
2 posts views Thread by special_dragonfly | last post: by
2 posts views Thread by dwasbig9 | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.