Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: impossible clause deleted

...

In this noncompliant code example, a std::fstream object file is constructed. The constructor for std::fstream calls std::basic_filebuf<T>::open(), and the default std::terminate_handler called by std::terminate() is std::abort(), which does not call destructors. Consequently, the underlying std::basic_filebuf<T> object maintained by the object is not properly closed, and the program has no way of determining whether an error occurs while flushing or closing the file.

Code Block
bgColor#FFcccc
langcpp
#include <exception>
#include <fstream>
#include <string>

void f(const std::string &fileName) {
  std::fstream file(fileName);
  if (!file.is_open()) {
    // Handle error
    return;
  }
  // ...
  std::terminate();
}

...