Dealing with an operator overload function failure

Say I have a class that requires dynamic allocation to implement a few of the operators. Take "+" for example; I need to create a new object to hold the sum of the two parameters whose size is not known at compile time.

I'm pretty sure the standard way to indicate a failure inside the overloading function would be to throw an exception. However I am currently involved in an embedded(ish) project where the spec. says no exceptions are to be used.

I think I have 2 options:

1. Return an "invalid" object (with a flag indicating an error has occurred) and check for this after each operation.
1
2
3
a = b + c
if (a.err)
  // handle error 

or
2. To forsake operator overloading entirely and think up a new way of doing things where all functions that involve dynamic allocation can return error codes. but this seems rather terrible too as I may end up with something like:
1
2
3
objA a
if (add(&a, b, c) == -1) // assuming b and c are initialized before this snippet starts
  // handle error 



Is there a number 3 that I haven't thought of? It seems that not allowing exceptions is fairly common even in the non-embedded world (http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Exceptions) so how is this normally done? or is operator overloading usually avoided when exceptions are not allowed?

Thanks for your time.
Last edited on
Google's style guide is considered bad pretty much everywhere outside Google.

Maybe you could do
 
objA objA::plus(const objA &b, unsigned *error_code = nullptr);
thanks, yer they even admit later in that document that they would use exceptions now if they started from scratch, unfortunately this isn't an option for me...
Last edited on
Set a global variable like it is done with http://linux.die.net/man/3/errno
Topic archived. No new replies allowed.