How would one implement a ternary comparison operator to determine, for example, the boolean value of a < b < c?
How would one implement a ternary comparison operator to determine, for example, the boolean
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Solution:
When coding a comparison, have the return type be a
comparisonobject that can chain additional comparisons, but is implicitly convertible to abool. This can even (kind of) work with types that weren’t coded with this intent, simply by casting them to thecomparisontype manually.Implementation:
A useful example:
Output:
Note: This was created by Mooing Duck, and a compiled, more robust example can be found on http://ideone.com/awrmK