FrozenHeart
FrozenHeart

Reputation: 20756

Why std::fstream returns void instead of bool

Why member function open of std::fstream class from C++ Standard Library returns void instead of bool for immediately checking succesful opening?

Upvotes: 0

Views: 137

Answers (2)

user1653241
user1653241

Reputation:

My understanding is that there are so many ways fstream could fail when its allowing you to attempt to read (or I suppose write also) any type of file. Also "failure" could be ambiguous depending on the situation and the programmer. Given that, I think it would be difficult to get anything meaningful out of a boolean return value.

Upvotes: 1

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181350

Because it throws exceptions when the open call was not successful if exceptions are set, or they use failbit to indicate an error occurred. From the documentation:

On failure, the failbit flag is set (which can be checked with member fail), and depending on the value set with exceptions an exception may be thrown.

Upvotes: 2

Related Questions