Of course, simply extending the try block to include using a is the easiest solution.
Also, if you really plan to exit(1) or otherwise abort the program on error, just don't put a try block here at all. An exception will be thrown, interrupting the program if it is not caught.
One option is to use std::optional . This is the same concept as using a pointer, but it uses automatic allocation and therefore you are less likely to create a memory leak. This is currently an experimental status; you can use boost::optional instead if your compiler does not have std::experimental::optional :
I would like to repeat, although this is a little spaghetti style, and it would be better to design your code in different ways so that you do not constantly check if the success or failure was built.
This requires ClassA be movable or copied. in_place is a special argument that calls the ideal forwarding constructor for the rest of the arguments. Without in_place you can only give the actual argument to the ClassA constructor; it does not consider implicit conversions in ClassA . (Since optional avoids the ambiguity between copying and initializing a list from an object of the same type).
source share