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
  • 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 4221 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…
  • Jan Cumps
    Jan Cumps over 5 years ago

    I now have a Eclipse Windows setup that supports C++ 20 extensions.

    I downloaded and unpacked Eclipse 2020-12 release C/C++ edition.

    I have Cygwin 64 bit installed, and upgraded GCC from 7.4 to 10.

    image

     

    I created a new "C++ Managed Build" project and selected Cygwin GCC.

     

    image

     

    Checked the Cygwin settings in the project properties:

     

    image

    Then set the dialect to C++2a, the preview of the not yet established standard:

     

    image

     

    Success. Here's a sorting example being debugged:

    image

    The code in the debug window is using the new <==> operator. It works.

    • 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

    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 better grip on the language as it does seem to be the default go-to for MCUs and I'd like to experience a wider set than just Arduino.

    All of the below is not C / C++ relevant. It's about housing code and data in memory.

    Not an issue for most computer developers. A key item for embedded development.

     

    I've been investigating linker scripts for several reasons lately. It's never an extremely happy story. Because it's a difficult subject that isn't language related. It's architecture and hardware stuff.

    Renesas tried to simplify it with a GUI. I see what they are getting at but it's not intuitive.

    plain GNU toolchain (I deliberately did not say GCC because the linker is language agnostic) has a linker script language that's closer to most other toolchains.

    Also not an easy thing, but Eclipse has a graphical view on it  (not an editor yet) that is excellent.

    In particular for microcontrollers that have scattered memory areas that are RAM, Flash, ROM, memory mapped chip sets, … that aren't managed by an OS.

    You can see there what goes where, or does not fit there. In this case a graph does wonders because you do not have to interpret and analyse logs to see what is too big to fit in an area.

     

    I'm often working with the Hercules controller family that has memory that can correct itself in case a sun flare hits a bit.

    Great functionality, but you have to train the linker how to  generate memory checksums, so that the microcontroller recognises an illegal bit flip in deep space.

    That's when you learn the deep deep trenches of linker files and memory assignment.

     

    But that ain't C / C++ image

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

    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 better grip on the language as it does seem to be the default go-to for MCUs and I'd like to experience a wider set than just Arduino.

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

    No, that's a really helpful point of view as I hadn't thought about it along those lines. Thanks. In fact that was the very reason I experimented with Go, which was to develop a desktop webapp (didn't get very far mind you). I think I must now give Rust a try.

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

    Andrew J  wrote:

    ...!  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.

     

    ...

    I had that too. The linker of the Renesas chain is not intuitive.

    But for this discussion: that's not a C / C++ thing. At that time the source code is already morphed into binary instructions.

    • 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