Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return throwing ternary operator? [duplicate]

Reading the documentation of the ternary operator, I've realized that there are two special cases that I've never used:

  • you can use it with functions that return void : bool ? void : void
  • you can throw inside a ternary operator

So is the following valid, completely defined, and oftenly used (assuming that this is a class member, and the class owns a Type _data[Size]) ?

Type& at(const unsigned int i) 
{
    return (i < Size) ? (_data[i]) : (throw std::out_of_range("ERROR"));
}
like image 376
Vincent Avatar asked May 15 '26 04:05

Vincent


1 Answers

Your example is valid and well-defined (assuming suitable definitions of Size and _data). As to "oftenly used" - I personally have never seen such a construct before, for what it's worth.

like image 65
Igor Tandetnik Avatar answered May 18 '26 23:05

Igor Tandetnik



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!