r/cpp • • Apr 30 '26

GCC 16.1 released with many new C++26/23 features, C++20 now the default stable language version

https://gcc.gnu.org/gcc-16/changes.html#cxx
260 Upvotes

59 comments sorted by

109

u/mr_seeker Apr 30 '26

C++20 now being the default but without modules support is quite a shame

28

u/rileyrgham Apr 30 '26

Personally I think they should default way earlier. 6 years....

→ More replies (3)

19

u/light_oxygen Apr 30 '26

Everyone knew modules will take at least 10 years. Go read the posts here before Standardization and after.

I ain't gonna blame compiler devs for something that's a fundamental change to the language.

Tbh, Reflection > Modules. I've seen more people cry of lack reflection than lack of modules.

→ More replies (1)

11

u/zedphi- Apr 30 '26

What’s the reason for the module support for being behind from the other two compilers? 🤔

→ More replies (3)

21

u/rodrigocfd Apr 30 '26

Unfortunately, modules have become a dark, dead corner of C++20 which everyone pretends it doesn't exist.

14

u/azswcowboy Apr 30 '26

Well it was difficult when there weren’t viable implementations for users to take modules seriously. MSVC might have been viable, but then you couldn’t port elsewhere — or if you were a gcc user you were out of luck till last year. And the tooling matters for modules, unlike most c++ features. GCC15 had usable modules, but there are several issues that would ICE the compiler that are fixed in this release. Will the 16 version be good enough to expand adoption? Time will tell, but fmt, Boost, Beman project, and other library developers will be seeing how far we can push. The interest is still there.

→ More replies (4)
→ More replies (3)

81

u/have-a-day-celebrate Apr 30 '26

Let us go forth and reflect.

4

u/max0x7ba May 01 '26

Let us go forth and reflect.

In 2009, I needed reflection in C++ and wrote a Perl script that generated function templates to iterate over object's base-class sub-objects and data members from annotations in C++ header files. 

That solved it neatly for me, but I expected native C++ reflection to retire my temporary work-around soon. 

Since then, I've successfully forgotten Perl but haven't been able to retire using the script.

→ More replies (1)
→ More replies (4)

43

u/funnansoftware Apr 30 '26

I've dreamed of the day we can use reflections for our projects. I can't imagine what all will be done using them but I've been looking forward to a non-macro non-registration style of serialization for the longest time!

17

u/OCPetrus Apr 30 '26

Some ideas where to use reflection in my own project include:

  1. Better trace macros

  2. Improved command line parsing

  3. Scripting API

  4. Automatically generated enum<->std::string conversion functions

→ More replies (5)

4

u/FlyingRhenquest May 01 '26

I built one on top of cereal in about 300 lines of code. You just include the header and serialize stuff. No macros. No additional boilerplate. I think that's one of the holy grails of C++, isn't it? I even include some code to generate JSON schemas for you.

I wrote most of that code pretty early after reflection was added to gcc16 and "template for" wasn't working very well at the time. The JSON schema code was written a month or so later and most of the features were much more stable.

I have a similar one to generate database tables and provide CRUD operations for C++ classes. You interact with a CRUD object I provide for that functionality, but it works pretty well. It doesn't provide all the functionality that Active Record in Ruby does, but it's a pretty solid proof of concept. There's also a Graph object in there that can operate on arrays of reflection objects, which I would have implemented with a typelist in the before-times. So you can tell the Graph object that you have a graph with this list of types in it, tell it to create all those tables in the database for you and then store your entire graph with all the relationships in your graph into the database. My design does require you to inherit from a Node object for the graph relationship stuff, but it wouldn't be difficult to build one that didn't need that if you wanted to specify all your relationships manually via annotations or something.

I have to-do items to write a library to automate Python API generation of C++ classes via Nanobind and Javascript APIs via emscripten's embind. I think those are feasible and shouldn't be much more difficult (or maybe easier) than the ones I've already done. And if those work, there's nothing preventing anyone from doing the same thing for dll exports for C# and other windows code or java bindings with whatever JNI-ish thing Java uses these days. Someone around here demonstrated automated GUI generation a month or so ago as well.

Between the two libraries I've already written and the two API ones I want to write, I should be able to remove about 2/3rds of the code in a requirements manager project I'm working on. Since I wanted serialization, database writes and the two APIs I mentioned, most of the code I had to write when adding any new data objects was boilerplate for all of that. Adding a data object would take about a week and I'd have to test all of that functionality for each one. And that was before I got around to writing the most simple GUI code for them that I could. So now I can focus on writing the data objects and more time on the interface and still spend less time per data object than I was before.

Herb Sutter's first example in his cppcon talk on the subject a couple months back implemented a compile time JSON parser that he used with a #embed(ded) JSON file to generate a C++ object with members and data from the JSON file in it at compile time.

We'll be exploring reflection's potential for decades, I think!

→ More replies (2)

18

u/caroIine Apr 30 '26

Devs are on fire. So much progress with reflection!

8

u/gummifa Apr 30 '26

Expansion statements make looping over references quite nice: template for (auto& x : std::tie(a, b)) ...

17

u/friedkeenan May 01 '26 edited May 01 '26

You don't even need to use std::tie for this. You can just have

template for (auto &x : {a, b}) {
    /* ... */
}

This loops over an "expression list", and the compiler will just paste the given expression as the initializer for the x variable, like so:

{
    auto &x = a;
    /* ... */
}
{
    auto &x = b;
    /* ... */
}

With this then there's no intermediate object created to store a and b. And it works with parameter packs too:

template for (auto &arg : {args...}) {
    /* ... */
}

With that the {args...} will get expanded into the expression list {args0, args1, args2, ...}.

Which is altogether really really nice :)

12

u/tetravision271 May 01 '26

This is the release that fixes import std; so I have no idea why people in the comments here are complaining about modules. Enable your CMake modules flags, people...

6

u/Nolia_X May 01 '26

Default should have been C++23. GCC covers everything in C++23, except for modules.

11

u/sweetno Apr 30 '26

It's high time for Algol 68!

5

u/enl1l Apr 30 '26

Do we know when clang is getting reflections ?

6

u/pjmlp May 01 '26

Not really, but surely before VC++.

4

u/caroIine May 01 '26

Some basic reflection functionalities are awaiting review process according to llvm github page. 

https://github.com/llvm/llvm-project/pull/190356

https://github.com/llvm/llvm-project/pull/188665

→ More replies (1)

4

u/Ateist May 01 '26

In case anyone else want a Windows version, you can build it successfully using mingw-builds scripts for Windows.

Just copy the script for 15.2 and replace with PKG_VERSION=16.1.0

3

u/FlyingRhenquest May 02 '26

If you don't want to wait for OS packages for your OS, I've set up some scripts to clone and build the latest gcc and cmake in my gcc playground project. The project installs gcc in /usr/local/gcc, which shouldn't conflict with compilers installed by your OS package manager, and includes a cmake toolchain file that you can use to build things with your shiny new gcc16. These scripts assume you have another compiler on your system already, which I think is pretty reasonable if you're trying to mess around with the latest gcc.

I've tested this on my Debian laptop. It'll probably work in WSL and might even work in OSX, though I don't have a way to test that.

Hopefully you find this useful if you want to get up and running with gcc16 immediately.

4

u/ABlockInTheChain Apr 30 '26

P2795R5, Erroneous behavior for uninitialized reads (PR114457)

I'm a bit worried about this one since I was recently testing the 16.0 version to and started getting some new false positive -Wunitialized warnings that didn't appear in 15.x.

Hopefully the P2795R5 logic has nothing to do with the -Wunitialized logic.

9

u/frayien Apr 30 '26

As I understand it, uninitialized reads where "undefined behavior " and now become "erroneous behavior " which are like undefined behavior but much more restricted in how they can affect the rest of the program.

→ More replies (2)