element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • 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 & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • 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
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • 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 Sorting in the C++20 release
  • 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: 13 Oct 2020 8:14 PM Date Created
  • Views 4320 views
  • Likes 6 likes
  • Comments 20 comments
  • cpp
  • c++
  • c++20
Related
Recommended

Sorting in the C++20 release

Jan Cumps
Jan Cumps
13 Oct 2020

I don't get over-excited about programming languages and their evolution easily.

But it happened this time. C++ is introducing a new dialect. The 2020 standard.

 

There are a number of things that are useful but I wouldn't consider them groundbreaking (watch cppcon videos for people that get excited but do not sound like they are excited).

 

Then there is the new sorting functionality.

In C++, you can overload the sort algorithm.

This is very handy. Developers can create sorting algorithms that can teach their objects to sort themselves.

In the past you had to generate equal, not equal, greater than, smaller than, greater or equal, smaller or equal.

Many times, you implemented two of those, and derived the other ones based on that.

 

There's a new comparator now: "<=>".

This one covers all situations. If it fits your scenario (I think it does in many cases), you get all the comparators implemented in one class member.

That means that it 'll be easer to write objects that plug in to the sorting algorithms of C++.

We all dream to write code that says if (thisObject < thatObject). Or use standard algorithms that can sort or compare our home-written objects.

I think that the <=> operator helps with this, by reducing the work for the class designer.

 

For the record: this does not cause any run-time or code-size cost. It's resolved at compilation.

I take the critique of developers that do not like abstraction: things are happening under the hood.

But no cost for memory, code size or performance. And a gain in understanding what you want to achieve in the code.

I'll post examples later.

 

If you want to start playing with the new options: Try out C++20 Preview: set up a development environment

  • Sign in to reply

Top Comments

  • shabaz
    shabaz over 5 years ago in reply to neuromodulator +3
    I too agree on the productivity despite the ugliness of some Python features : ) And the perpetual messy changing ways for concurrent programming with Python. I've only used C++ in a large real-time app…
  • Jan Cumps
    Jan Cumps over 5 years ago in reply to Andrew J +3
    Andrew J wrote: You're right, a specific toolchain isn't applicable to the source code, I digressed into the wider C/C++ ecosystem which wasn't the point of your original post. I do need to get a much…
  • neuromodulator
    neuromodulator over 5 years ago +2
    C++ has turned into such a complex language and gets more complex at every release. Today not even Stroustrup knows every feature of the language. The other aspect that I don't like much about C++, is…
  • Fred27
    Fred27 over 5 years ago in reply to BigG

    I don't see Go and Rust as direct competitors. They both have their strengths and appropriate uses. Rust would be a far better choice for embedded and competes with C/C++. Go has a garbage collector and large libraries so is not deterministic enough and takes up too much space for embedded. Go competes with Java or C#, and would probably be a better choice than Rust for a website or desktop app. Obviously these are very broad brush opinions. Feel free to disagree.

     

    Sorry for dragging your C++ post a bit off topic, Jan!

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • BigG
    BigG over 5 years ago in reply to Fred27

    Interesting that Google v Microsoft has come down to a battle of language preferences too (i.e. Golang v Rust).

     

    I've not dabbled in Rust yet so was intrigued to see how the two handle sorting. Just pulled two examples off the net.

     

    https://oswalt.dev/2020/05/beyond-hello-world-sorting-algorithms-in-rust/

    https://www.tutorialdocs.com/article/golang-sort-algorithms.html

     

    Still, I'm not sure which is better and if these are better / worse than the new c++ option.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Fred27
    Fred27 over 5 years ago

    Despite being a professional C# developer since C# was in beta (over 20 years ago), I've dabbled in C++ but it's never really grabbed me. C seems to suit me better for embedded and C# for bigger stuff.

     

    Rust is looking really nice for low level stuff. I've had a play and it's on my list of things to learn. It does seem to be really taking off - especially with Microsoft starting to use it for internal development of Windows, etc. Right now there doesn't seem to be much in the way of jobs that mention Rust though. Also, whilst embedded Rust development is possible and you can use Visual Studio Code as an IDE and debugger, none of the big players have integrated it into their own toolchains yet.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Andrew J
    Andrew J over 5 years ago in reply to Jan Cumps

    I can’t remember the precise details Jan.  I had a working example from Renesas and I was trying to do something similar with my own example and couldn’t figure out why their code worked and mine didn’t.  I had a hunch that it may be a compiler or linker setting (at least having 30 years under my belt I can recognise problem symptoms!!) and after looking for anything obvious ended up trawling through, entry by entry, the compiler and linker configuration dialogs comparing the two.

     

    It was the Renesas toolchain.  The big head ache was that it wasn’t obvious how the individual dialogs mapped to the final compiler/linker commands as there were some parameters that didn’t appear to have a settings dialog!  I more or less stumbled across it in the end and IIRC it was something to do with a memory allocation starting address for one of the blocks.  I know you wrote a whole post about setting the allocation up for a more complex/memory intensive build which was useful but wasn’t directly related, or didn’t appear to be.  Their code, in their project worked; their code copied into a new project didn’t and it came down to just one setting in the compiler or linker and the default settings for a new project needed changing.  That then became a new template starting point for examples I created.

     

    Obviously, some of this is down to unfamiliarity with C/C++ and associated tools on my part (as DAB points out below) but it still feels unnecessarily complex in the year > 2000.  Where I just crack-on with languages like Java, C# or Swift, I still have to hit Google with C/C++ to figure out *, &, array comparison, OO and so on.  I think frustration leads to not remembering enough, my mind blanks out the hideousness of it all image

     

    Anyway, I’m not intending to denigrate the language as it is widely used and can create compact, fast applications, useful for memory constrained MCUs; I’d just argue the toss about using the words ‘simple’ and ‘c/c++’ in the same sentence image

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 5 years ago in reply to Andrew J

    When working with the R65N, were you struggling with the Renesas C/C++ toolchain and the memory allocation screen? Were those the settings you had to copy?

    • 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 © 2026 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