If the resource-allocation part of the constructor of e.g. a RAII socket wrapper fails, do I just throw an exception and be done with it? Or should I go with how std::fstream
seems to do it, where you need to check is_open()
after you construct the object?
The former seems more in-line with the name "resource allocation is initialization", but then why does the standard library basically make you check an error code before you use the object?
I am referring to the example on the reference page for basic_fstream
(paraphrased, notes added):
int main() {
std::string filename = "test.bin";
std::fstream s(filename);
if (!s.is_open()) { // should my socket class require the user to do this?
std::cout << "failed to open " << filename << '\n';
} else {
// ... use it
}
}