Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ can an int function return something like "NULL"?

I have a function that computes something and if the result is not valid i want to return something like "NULL". I have found a way using pointers, but is there an easier way to do that?

like image 562
maria Avatar asked Dec 09 '25 22:12

maria


1 Answers

It really depends on the semantics of your function. If the function's contract is that it will return an int to the user and for some reason it is unable to do so, then your function should be throwing an exception. If the function's contract is that it will either return an int or nothing to the user, then you might want to use boost::optional.

like image 180
Joseph Mansfield Avatar answered Dec 11 '25 12:12

Joseph Mansfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!