The ternary operator has a special rule when one side is a throw
. Part of that rule is that the result always will be a temporary rvalue, never a reference. So the compiler needs to make a copy of the getline()
return value, and this fails because the stream is non-copyable.
You can explicitly discard the getline()
return value in order to avoid the copy attempt:
inFile? (void)getline(inFile, inLine) : (throw -2);
I don't agree with your cleanup attempt, however. The original is easier to understand and maintain.
Also, did you mean to test goodness of inFile
before or also after getline()
? You could do
getline(inFile, inLine) || (throw -2);
which is idiomatic is some other languages, like perl's something or die "Error message"