
On 10/31/09 04:49, Gottlob Frege wrote:
bool diff(file srcA, file srcB, file output) { // get our files ready... error_code srcA_err = file_open(srcA); error_code srcB_err = file_open(srcB); error_code out_err = file_open(output);
if (srcA_err || srcB_err || out_err) // check for errors return false;
// do the diff...
return true; }
So that took a bit of thought for me to come up with, but the code doesn't look completely unreasonable, does it? Hmmm, that depends on your background. If you look at a respective POSIX program:
int srcA_err = open(srcA); int srcB_err = open(srcB); This will overwrite errno. So system level programmers are used to write: int err = open(srcA); check_error(err, "first source file"); err = open(srcB); check_error(err, "second source file"); So this specific issue should be teachable.
Basically, you never want more than one actual error_code to exist at a time (per thread). Hmmm, what about:
void do_the_diff(file &srcA, file &srcB, file &out) { error_code readA_err, readB_err, out_err; // chunksize, chunkA, chunkB bytes_read = read(&readA_err, srcA, &chunkA, chunksize); // check for and handle EINTR (only) bytes_read = read(&readB_err, srcB, &chunkB, chunksize); // check for and handle EINTR (only) // ... } Here we would throw in case any of the error_code object would have another error than EINTR. This becomes more interesting both contain such an error. I'm not so sure that I like this approach anymore... Detlef