0

I develop a library that internally uses Boost.IOStreams. Recently, I hit an interesting corner case with file_descriptor_sink. It looks like doing

file_descriptor_sink s("");

or

file_descriptor_sink s;
s.open("");

is perfectly valid and s.is_open() definitely returns true afterwards. For me, this is a complete nonsense. That is to what file is it open?

Two questions in this regard:

  1. Is there any function that I could use to detect this condition? To elaborate, when I first hit this case, I immediately recalled that the following holds:

    ofstream ofs("");
    assert(!ofs);
    assert(!ofs.is_open());
    assert(ofs.fail());
    

    what is clearly opposite to the file_descriptor_sink behavior in terms of is_open and fail is not even available. Otherwise, I'd have to directly test for empty string (pathname) before construction of file_descriptor_sink in order to track this condition.

  2. Do you agree that throwing well-forged exception to a user of the library in this case would be a good idea? I was thinking about std::system_error with the bad_file_descriptor error code.

Alexander Shukaev
  • 16,674
  • 8
  • 70
  • 85

0 Answers0