In C++, how to handle function outputs , if the inputs are invalid. Say, I am writing a function to multiply two matrices. How to deal with it if the two matrices cannot be multiplied (due to in-compatible shape)?
I tried to use std::optional , but then it has created other problems. Since the output is expected to be std::optional(Matrix) ,I have to write all the other functions in the class (like print Matrix()) with the expected output as std::optional , which seems unnecessary.
std::optionalstrikes me as unnecessary, but if that's what you're required to do ....