I am looking for a way to set a warning that the caller will have to respond to. In a sense I would like to use a late exception mechaninism that occurs after the function already finished executing and returned the wanted value.
SomeObject Foo(int input)
{
SomeObject result;
// do something. oh, we need to warn the caller.
return result;
}
void Main()
{
SomeObject object;
object = Foo(1); // after copy constructor is done I would like an exception to be thrown
}
You have to make a more concrete decision, I think. It’s very unorthodox to (somehow) warn the user of a function while giving them a result.
For example, you could return a
std::pair<SomeObject, std::string>, where the warning is in the string, if any. But it’ll be very easy for people to ignore that.An exception isn’t the solution: if you can continue execution in a meaningful way, it’s not an exceptional situation. It’s possible to come up with some system of letting them finish with an exception “in queue” somewhere, but when should it be thrown? And for what purpose? Surely it will end up cutting of the remainder of the work anyway.
I think what you want is an
assert.assertthat a condition holds true; if it doesn’t, the programmer will be notified and can handle the situation accordingly. Remember,assert‘s are for programmers, exceptions are for the program.Perhaps if you give us a more concrete example, we’d be able to suggest the best course of action.