This is not actually a 2-stage destruction. Closing a stream is a change of stream state, not a phase of destruction.
Technically, I could have changed the first code sample to
Code:
void FunctionToGiveAnAdditionalContainingScope()
{
ifstream file2("file2.txt");
{ // begin the scope as per your example
ifstream file1 ("file1.txt");
//use file1 and file2 here
}
// use file2 here
} // file2 stream's destructor invoked here, so the stream will be closed.
but decided I didn't want to get into the debate if you were to adjust the example so the filename for file2 became derived from data in file1.
Incidentally, I just realised I could have also used the rule that "automatic destruction of objects due to scope is in the reverse order of their construction" and produced this
Code:
void FunctionToGiveAnAdditionalContainingScope()
{
ifstream file1 ("file1.txt");
// read data from file1
ifstream file2(some_filename_derived_from_data_read_from_file1);
//use file1 and file2 here
file1.close(); // this is strictly optional
// use file2 here
} // file2 stream's destructor invoked here, then file1's destructor, so the streams will be closed if not already closed.