Liu WeiPeng
Posts: 4
Nickname: pongba
Registered: Mar, 2005
|
|
Re: Conditional Love: FOREACH Redux
|
Posted: Mar 23, 2005 8:50 PM
|
|
Despite of my absolute loving this facility,I wonder if there exists one little pitfall.
It seems to me that the problem raises its ugly head when we use BOOST_FOREACH for some container that contains instances of type like std::string or whatever owns its own resource.
IMO,some classes that hold their own resources will do copy assignment differently as they do "destruction-then-in-place-construction".
Since BOOST_FOREACH uses nested for-statement,the variable in the inner loop will get initialized each time the outer for-loop recurs.Note that I refered to "initialize" instead of "copy-assignment",the former means a destruction followed by a construction,while the latter means merely one copy assignment.
For me,the situation can become really tough when the class gets a optimized copy assignment which can reuse the previously allocated storage to hold the new stuffs,while its destructor *delete* the storage and its constructor *re-require* them.
e.g.
for(string s = ...;...;...) { s = ...; // copy assignment , the lifetime of s lasts until the for-statement ends }
// BOOST_FOREACH's implementation for(...) { for(string s = ...;...;...) // get re-constructed each time the outer loop recurs { } }
Surely,I know that there should be some caveat when using BOOST_FOREACH,one of which should refer to this.But I read through the document and see nothing about that,so I wonder if it's an oversight,or "not-a-problem",or merely my misunderstand?
Anyway,thank you for doing such an excellent job,it's really usefull and amazing.
|
|