element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog C++ 3 way compare
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Jan Cumps
  • Date Created: 15 Jul 2025 3:49 PM Date Created
  • Views 322 views
  • Likes 8 likes
  • Comments 5 comments
  • Modern C++
  • spaceship
  • c++26
  • c++23
  • c++
  • c++20
Related
Recommended

C++ 3 way compare

Jan Cumps
Jan Cumps
15 Jul 2025

task: Let's say that you are writing a C++ class, where the objects have to be sorted or compared.

In classic C++, you 'd write an operator overload for <, <=, == , !=, >= and >. In modern C++, you can get all that functionality with a single method: <=> (aka the spaceship operator).
If you provide an implementation for that operator, the compiler will derive all comparisons.

class compare_me {
 public:
    constexpr compare_me(int value): value{value} { }
    auto operator<=>(const compare_me& rhs) const = default;  
 private:
    int value;
};

I'm using C++ default <=> implementation here. It will compare all the class data members - if they support comparison. In this example, the operator will compare the value data member.
If you can't (or don't want to) use that functionality, you can provide your own implementation. For the rules on how default works, see Default comparisons order.

This is functionality you get when providing this single operator overload in your class:

    constexpr compare_me value_1(1);
    constexpr compare_me value_2(2);
    constexpr compare_me other_value_2(2);
    constexpr compare_me value_3(3);
    
    std::cout << (value_1 < value_1) << std::endl;
    std::cout << (value_1 < value_2) << std::endl;
    std::cout << (value_2 < other_value_2) << std::endl;
    std::cout << (value_2 <= other_value_2) << std::endl;
    std::cout << (value_2 >= other_value_2) << std::endl;
    std::cout << (value_2 == other_value_2) << std::endl;
    std::cout << (value_2 != other_value_2) << std::endl;
    std::cout << (value_2 <= other_value_2) << std::endl;
    std::cout << (value_3 < value_2) << std::endl;
    std::cout << (value_3 <= value_2) << std::endl;
    std::cout << (value_3 > value_2) << std::endl;
    std::cout << (value_3 >= value_2) << std::endl;

Result:

false
true
false
true
true
true
false
true
false
false
true
true

Complete code:

#include <compare>
#include <iostream>

class compare_me {
 public:
    constexpr compare_me(int value): value{value} { }
    auto operator<=>(const compare_me& rhs) const = default;  
 private:
    int value;
};

int main() {
  
    constexpr compare_me value_1(1);
    constexpr compare_me value_2(2);
    constexpr compare_me other_value_2(2);
    constexpr compare_me value_3(3);

    std::cout << std::boolalpha << std::endl;
    
    std::cout << (value_1 < value_1) << std::endl;
    std::cout << (value_1 < value_2) << std::endl;
    std::cout << (value_2 < other_value_2) << std::endl;
    std::cout << (value_2 <= other_value_2) << std::endl;
    std::cout << (value_2 >= other_value_2) << std::endl;
    std::cout << (value_2 == other_value_2) << std::endl;
    std::cout << (value_2 != other_value_2) << std::endl;
    std::cout << (value_2 <= other_value_2) << std::endl;
    std::cout << (value_3 < value_2) << std::endl;
    std::cout << (value_3 <= value_2) << std::endl;
    std::cout << (value_3 > value_2) << std::endl;
    std::cout << (value_3 >= value_2) << std::endl;
              
    std::cout << std::endl;
              
}

Bonus: use STL sorting algorithm on a container of our objects

This is functionality that you get when you provide the <=> operator.

Example: execute a few STL algorithms on an an array of (initially) unsorted objects 

#include <array>
#include <algorithm>



    std::array<compare_me, 4> sort_me {2, 3, 2, 1};
    for(const auto& cm : sort_me) { std::cout << cm << " "; }
    std::cout << std::endl;
    std::cout << "sorted? "<< std::is_sorted(sort_me.begin(), sort_me.end()) << std::endl;
    std::sort(sort_me.begin(), sort_me.end());
    for(const auto& cm : sort_me) { std::cout << cm << " "; }
    std::cout << std::endl;
    std::cout << "sorted? " << std::is_sorted(sort_me.begin(), sort_me.end()) << std::endl;

Output:

2 3 2 1
sorted? false
1 2 2 3
sorted? true

sorting algorithms used:

std::is_sorted()
std::sort()

By default, STL uses the < operator to sort. If you want to sort using other criteria, that's possible.
This snippet sorts the container from large to small:

std::sort(sort_me.begin(), sort_me.end(), [](const compare_me& a, const compare_me& b) {
    return a > b;
});

for (const auto& cm : sort_me) { std::cout << cm << " "; }
std::cout << std::endl;

std::cout << "sorted? " << std::is_sorted(sort_me.begin(), sort_me.end(), [](const compare_me& a, const compare_me& b) {
    return a > b;
}) << std::endl;

3 2 2 1 
sorted? true

Bonus 2: print a container of our objects

To support printing, I also provided an << operator for my class compare_me:

    friend std::ostream& operator<< (std::ostream& os, const compare_me& cm) {
        return os << cm.value;
    }

You can then print an individual object to a stream (in this case: the standard output stream. 
To print the full array of objects, you can use this:

    for(const auto& cm : sort_me) { std::cout << cm << " "; }

Inspiration: MC++ 

attached: full code main20250719.zip

Thank you for reading.

  • Sign in to reply

Top Comments

  • DAB
    DAB 1 month ago +1
    Nice algorithm Jan.
  • Jan Cumps
    Jan Cumps 1 month ago in reply to Jan Cumps

    example 2: sorting.

    (note: I did not have to write the algorithm):

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 1 month ago in reply to Jan Cumps

    example 1: use STL to check if an array is sorted:

    image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps 1 month ago

    one of the advantages of this mechanism, is that the STL built-in sorting algoritms work.

    If you have a container (e.g.: array, vector, list,...) of your objects, you can tell STL to sort them.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • kmikemoo
    kmikemoo 1 month ago

    That is SUPER cool! Thanks for sharing, Jan Cumps .

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB 1 month ago

    Nice algorithm Jan.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube