Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8073907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:29:19+00:00 2026-06-05T14:29:19+00:00

How would one implement a ternary comparison operator to determine, for example, the boolean

  • 0

How would one implement a ternary comparison operator to determine, for example, the boolean value of a < b < c?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:29:20+00:00Added an answer on June 5, 2026 at 2:29 pm

    Solution:
    When coding a comparison, have the return type be a comparison object that can chain additional comparisons, but is implicitly convertible to a bool. This can even (kind of) work with types that weren’t coded with this intent, simply by casting them to the comparison type manually.

    Implementation:

    template<class T>
    class comparison {
      const bool result;
      const T& last;
    public:
      comparison(const T& l, bool r=true) :result(r), last(l) {}
      operator bool() const {return result;}
      comparison operator<(const T& rhs) const {return comparison(rhs, (result && last<rhs));}
      comparison operator<=(const T& rhs) const {return comparison(rhs, (result && last<=rhs));}
      comparison operator>(const T& rhs) const {return comparison(rhs, (result && last>rhs));}
      comparison operator>=(const T& rhs) const {return comparison(rhs, (result && last>=rhs));}
    };
    

    A useful example:

    #include <iostream>
    int main() {
      //testing of chained comparisons with int
      std::cout << (comparison<int>(0) < 1 < 2) << '\n';
      std::cout << (comparison<int>(0) < 1 > 2) << '\n';
      std::cout << (comparison<int>(0) > 1 < 2) << '\n';
      std::cout << (comparison<int>(0) > 1 > 2) << '\n';
    }
    

    Output:

    1
    0
    0
    0
    

    Note: This was created by Mooing Duck, and a compiled, more robust example can be found on http://ideone.com/awrmK

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How should one implement operator==(const Base& base) to compare subclasses s.t. the calls would
How would one implement LINQ to extract the Guid's from one collection of objects
How would one typically implement a service layer in an MVC architecture? Is it
I would like to use Akka to implement a multiple-readers / one-writer pattern on
I would like to implement an distributed Point-Of-Sale system, somewhat like the one described
How would one shorten the following using ternary operators? if ((pos - maxPos) ==
I'm wondering how one would implement an outline view like the one Xcode 3
I was considering how one would implement some voting mechanism, using AJAX, that limits
How would one implement INotifyPropertyChanged for use in an F# type? Thanks!
For what reasons would one choose several processes over several threads to implement an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.