I'm doing something similar to this item Correct BOOST_FOREACH usage?
However, my returned list is wrapped in a boost::shared_ptr. If I do not assign the list to a variable before the BOOST_FOREACH loop, I get a crash at runtime as the list is getting destructed as it is a temporary.
boost::shared_ptr< list<int> > GetList()
{
boost::shared_ptr< list<int> > myList( new list<int>() );
myList->push_back( 3 );
myList->push_back( 4 );
return myList;
}
Then later..
// Works if I comment out the next line and iterate over myList instead
// boost::shared_ptr< list<int> > myList = GetList();
BOOST_FOREACH( int i, *GetList() ) // Otherwise crashes here
{
cout << i << endl;
}
I would like to be able to use the above without having to introduce a variable 'myList'. Is this possible?