September 7th, 2024

What's new in C++26 (part 1)

C++26 is developing features like specifying reasons for deleted functions, unnamed placeholder variables, structured binding in control statements, and user-generated messages in static_assert, enhancing code clarity and usability.

Read original articleLink Icon
What's new in C++26 (part 1)

C++26 is currently under development, introducing several new language and library features. One significant addition allows developers to specify reasons for deleting functions, enhancing clarity for API authors. This feature enables a more informative error message when a deleted function is invoked, rather than a generic compiler error. Another notable change is the introduction of unnamed placeholder variables using a single underscore (_), which can simplify code by allowing the declaration of variables that are not used. This feature also automatically applies the [[maybe_unused]] attribute to such variables, preventing compiler warnings. Additionally, C++26 permits structured binding declarations in the conditions of if, while, or for statements, which streamlines code by allowing multiple variables to be declared and initialized in a single statement. Lastly, the static_assert feature has been enhanced to accept user-generated string-like objects for error messages, improving the flexibility and clarity of compile-time assertions. These updates aim to improve code readability and usability in C++ programming.

- C++26 introduces the ability to specify reasons for deleted functions.

- Unnamed placeholder variables can now be declared using a single underscore (_).

- Structured binding declarations can be used in the conditions of control statements.

- User-generated string-like objects can be used in static_assert messages.

- These features enhance code clarity and usability in C++.

Link Icon 18 comments
By @wsve - about 1 month
I often feel that when C++ posts come up, the majority of commenters are people who haven't deeply worked with C++, and there's several things people always miss when talking about it:

- If you're working in a large C++ code base, you are stuck working with C++. There is no migrating to something like Rust. The overhead of training your engineers in a new language, getting familiar with a new tool chain, working with a new library ecosystem, somehow finding a way to transition your code so it works with existing C++ code and isn't buggy and adapts to the new paradigms is all extremely expensive. It will grind your product's development to a buggy halt. It's a bad idea.

- Every time a new set of features (e.g. reflection, concepts, modules, etc.) is released, people bemoan how complicated C++ continues getting. But the committee isn't adding features for the sake of adding features, they're adding features because people are asking for them, they're spending years of their lives writing papers for the committee trying to improve the language so everyone can write better code. What you find horrifying new syntax, I find a great way of fixing a problem I've been dealing with for years.

- Yes, it's a gross homunculus of a language. If I could switch our team to Rust without issues, I would in a heartbeat. But this is the beast we married. It has many warts, but it's still an incredible tool, with an amazingly hard working community, and I'm proud of that.

By @jll29 - about 1 month
C++ is a monster.

The 2026 proposal has some neat ideas (I like the ability for the developer to give a reasons for modifying behavior that may create otherwise cryptic error messages, for instance); but the more things one packs in there, the uglier, bloated the specs, and the more complicated and buggy compilers will be.

Once C with Classes was an experimental pre-processor to try out bringing in some Simula ideas into the C world. Today, C++ has become a language that changes dramatically every half a decade, where the main question is "will it compile" if you receive someone else's code, and where even experienced developers cannot tell from compiler error messages what's wrong (g++). The undoubtedly clever people who have been working on it have nevertheless committed war crimes in anti-orthogonality.

Tip: introduce a versioning mechanism like Rust has it, so that you are freed from the burden of having to be backwards-compatible.

By @pjmlp - about 1 month
Exciting as it may be, to be fully available for portable codebases maybe around 2030, given the current velocity of compilers adoption of ongoing standards, even among the big three.

As of today, C++17 is the latest one can aspire to use for portable code, and better not making use of parallel STL features.

By @sohamgovande - about 1 month
One major gripe I have with these C++ updates is that the proportion of codebases that actually use recent C++ features (cpp17, cpp20, cpp23) is very close to zero. The more and more esoteric this language becomes, the fewer people who can actually master it.

Source: I've been writing C++ for 8 years.

By @sixthDot - about 1 month
> if (auto [to, ec] = std::to_chars(p, last, 42))

I'm not into plusplus, however i'm curious. How the tuple get evaluated to a condition ? is that lowered to if `to && ec` ?

By @omoikane - about 1 month
User-generated static_assert messages would make it easier to build games that can be played entirely using compiler error messages. Something like this old IOCCC entry but nicer:

https://www.ioccc.org/years.html#1994_westley

By @logicchains - about 1 month
This doesn't mention the most exciting thing coming: static reflection. Finally no need to manually implement printing or serialisation functions for every struct.
By @giancarlostoro - about 1 month
Some of those features look like features I've been seeing in all major languages I use. They're mostly ergonomic for the developer.
By @porphyra - about 1 month
C++ is a funny chimeric creation that has absorbed some great modern ideas from Rust and other new language but needs to preserve compatibility with its antediluvian C heritage. You could write it in a very clean and somewhat safe modern style or in a familiar C-like style. We use modern C++ at work and, by embracing RAII, it really isn't so bad.
By @alecco - about 1 month
By @rldjbpin - about 1 month
how long before you reckon it would be widely adopted? i feel like the pace of revision (~3 years per standard) seems too fast.
By @SuaveSteve - about 1 month
Regarding the delete feature, can one not just raise in C++ for a deprecated/deleted function?
By @mckravchyk - about 1 month
I for one started working on a new project in C++ rather than Rust. I think it's unclear whether Rust is going to be the successor at this point. It's probably never going to pick up in the games industry, QT is C++ (and Rust bindings will always be second class or they could end up unmaintained), has better compile times and is said to be undisputed when it comes to high performance. Obviously the tool for the job factor is most critical.

Career wise, many people are picking up Rust and almost no one is picking up C++, while experienced C++ devs either retire or switch to a higher level language due to landscape change. I would trust supply and demand to be in favour of C++ 10 years from now.

There are also attempts to make C++ more memory safe like Carbon[1] or Circle compiler [2]. If they succeed, why would anyone want to switch to Rust? Also Rust is not ideal for security from a different perspective - I think the lack of a package manager is one C++ strongest points. After working for 9 years with npm, you really appreciate that the dependency you install is usually just a single dependency (even an option for a single header file is common), when there's a package manager, people will abuse it (like install a package with 3 dependencies of its own for something that could be 50 LOC copy-paste) and managing the security of the supply chain is nearly impossible (it will be a legal requirement soon in EU though).

Anyway, I wanted to ask. How is the contracting market looking in C++ world? I'm guessing it depends on the domain heavily? I'm mainly asking about QT and anything that would be desktop / mobile apps / systems programming except video games, but I'm curious in general.

[1] https://github.com/carbon-language/carbon-lang

[2] https://github.com/seanbaxter/circle

By @binary132 - about 1 month
Reflection is awesome, it reminds me a lot of Zig’s comptime functionality.
By @wseqyrku - about 1 month
Why they don't just invest in carbonlang instead?
By @psyclobe - about 1 month
C++, the sharpest knife in the drawer.
By @bun_terminator - about 1 month
.
By @roenxi - about 1 month
Leading with "Specifying a reason for deleting a function" then following up with "Placeholder variables with no name" did make me check the date of the article. It wasn't April 1.

The standards committee are thorough in their mission to including everything and the kitchen sink in C++.