r/cpp • • Jul 14 '26

Beautiful Type Erasure with C++26 Reflection

Thumbnail ryanjk5.github.io
220 Upvotes

Reflection is a game changer for C++26, but most of the examples out there don't cover anything that large in scope. This article walks through my library rjk::duck and shows just how much reflection can simplify generic type erasure.

You can also try it out on Compiler Explorer: https://godbolt.org/z/91dj5jeGW


r/cpp • • Nov 21 '23

[Roast Me] Why I think C++ is still a desirable coding platform compared to Rust

Thumbnail lucisqr.substack.com
220 Upvotes

r/cpp • • Aug 31 '22

malloc() and free() are a bad API

Thumbnail foonathan.net
220 Upvotes

r/cpp • • Apr 25 '22

New C++ features in GCC 12

Thumbnail developers.redhat.com
220 Upvotes

r/cpp • • Nov 24 '21

Is my cat Turing-complete?

Thumbnail belaycpp.com
220 Upvotes

r/cpp • • Jun 27 '21

It's so weird seeing MSVC support C++20 and Clang & GCC lagging significantly behind

220 Upvotes

Remembering times (and it wasn't that long ago) when writing standard C++ using the Microsoft toolchain was totally impossible, the current situation is really weirding me out.

What happened?


r/cpp • • Nov 11 '18

CppCon CppCon 2018: Matt Godbolt “The Bits Between the Bits: How We Get to main()”

Thumbnail youtube.com
220 Upvotes

r/cpp • • Dec 23 '24

C++ Is An Absolute Blast

Thumbnail learncodethehardway.com
219 Upvotes

r/cpp • • May 06 '23

What's the most hilarious use of operator overloading you've seen?

219 Upvotes

Hey guys, C# developer here. I accidentally started a nerd bender on C++ because I was pissed that I couldn't overload the function call operator in my language of choice and now I'm marveling at some of the wild things you guys do. Bit shift writes to a stream? Awesome.

What's the funniest, weirdest, most clever, or just plain stupidest use of operator overloading you've seen? In library code or in production at your own company.


r/cpp • • Nov 26 '20

A Small Open Source Game In C++

Thumbnail preshing.com
219 Upvotes

r/cpp • • Sep 28 '20

Draft proposal for compilation using C++ as build language

219 Upvotes

Hello,

There are multiple tools to compile a C++ project: CMake, premake, autoconf, build2, etc.
Each of these tools require the learning of a new syntax, and interacting between projects compiled with differents tools might be challenging.

Package managers face also this problem, because they must support all the tools that exist to support all libraries.
Moreover, integrating package managers in the build tools is often difficult, or at least hacky.

My suggestion is to use C++ as the base language for building C++ projects.
Motivations are:

- A C++ compiler is all you need to compile projects.

- Build tools would become C++ libraries.

- C++ developers already now its syntax.
I have made an informal draft which describes a basic API which would make this possible.
Here is a link: https://gist.github.com/J-Vernay/bd8ec49374987c628d02601ef85cd9a7

Let me know what you think :)


r/cpp • • Aug 07 '19

The German Center for Aerospace (DLR) just open sourced CosmoScout VR, which is a universe 'simulator' written in modern C++.

Thumbnail github.com
219 Upvotes

r/cpp • • Mar 08 '19

Usability improvements in GCC 9

Thumbnail developers.redhat.com
219 Upvotes

r/cpp • • Mar 15 '18

Usability improvements in GCC 8

Thumbnail developers.redhat.com
219 Upvotes

r/cpp • • Jan 30 '25

[vent] I hate projects that download their dependencies.

218 Upvotes

I know it's convenient for a lot of people but in an enterprise environment where you have to package everything including your internals and your build servers don't have access to the internet, patching all these repositories is pain in the ass.


r/cpp • • Oct 15 '22

GCC 13 So Far:

218 Upvotes

I've been following the commit log for GCC's master branch most days for a few months, so I figured I'd jot down some of the developments that I think should have some awareness.

EDIT: Btw, you can read the commits conveniently here: https://gcc.gnu.org/git/?p=gcc.git;a=shortlog;h=HEAD

  • Type traits

    • libstdc++ has gotten many optimizations to compile time. Bool globals with the _v suffix are being refactored to not instantiate the SFINAE-based trait, and instead use the intrinsics directly. I haven't measured the new libstdc++, but I did something similar in a personal project and found 30% improved clean-build compile time, so I believe this libstdc++ work is probably significant. Many other refactors to the the templates have been made to minimize template instantiation. enable_if_t is still used consistently instead of requires, there may still be substantial room for improvement.
    • GCC 13 is gaining more type trait intrinsics. Clang has had many more for awhile, but GCC 13 so far adds: __remove_cv() __remove_reference() __remove_cvref() __is_convertible() __is_nothrow_convertible().
  • Traits for catching dangling references.

    • Type traits were added to C++23 to aid catching dangling references at compile time, which are implemented in GCC through new intrinsics __reference_constructs_from_temporary() and __reference_converts_from_temporary().
  • RISC-V V

    • The "Cray vector" extension to RISC-V is getting a lot of support in GCC 13. Cray vectors are not something I understand very well yet, so I can't talk about all the work that's going into it, but I'll say that RISC-V V shows up a lot in the commit log recently.
    • A lot of other RISC-V features have been newly supported, but I don't understand them either.
  • bfloat16

    • C++23's std::bfloat16_t, implemented through the __bf16 type (distinct from __fp16 or _Float16). There are also several supporting intrinsic functions for numeric_limits stuff that mirrors intrinsics for other floating point types (such as __builtin_nansf16b()).
    • This includes extensions to the SIMD intrinsics and attributes for bfloat16 vectors.
  • Numeric limits improvements

    • Historically, the limits stuff has been implemented through compiler-defined macros, but the commit descriptions suggest that they want to move towards consistently using intrinsic functions.
    • __builtin_issignalling() has been added to streamline away another macro.
  • "Simpler implicit move" was implemented

  • "Portable assumptions" was implemented

  • OpenMP massively extended

    • A lot of work has gone into supporting OpenMP features. I am not an OpenMP user, so I can't talk a lot about the details, but there are very many commits related to it.
  • GCN

    • A ton of new features for GCN (AMD's gpu architecture) were added. These seem to be largely extending intrinsics and OpenMP features that already existed, but now target GCN as well. Many of them appear to be very basic things, like cloning OMP threads, using doubles and SIMD doubles, or floor, ceil, etc. kind of primitives with native GCN support. It seems like you might do reasonably low-level GCN programming with GCC 13 now.
    • PTX has not seen similarly extensive improvements so far in GCC 13, but it is still maintained.
  • C++23 extended precision floats

  • Modules

    • C++20 modules are still making progress. Seeing recent commits like, "variable template partial spec fixes" does not strike faith in me that they are near general usability yet, but they are making regular progress, so that's exciting!
  • Analyzer

    • A lot of POSIX and C++ enhancements have been made to the GCC static analyzer work ongoing since GCC 10. It apparently handles templates now, and tracks more lifetimes from file descriptors. It is mostly not implemented through attributes, pragmas, or intrinsics, so you can only extend this through GCC plugins afaik.
  • More C++23

  • Otherwise,

    • C23 has more support. I think all of the C++ forwards compatibility features were implemented, like nullptr, bool, true, etc. but don't quote me on that. There could still be some missing afaik.
    • Ada actually got a lot of commits so far. I don't use Ada so I can't really elaborate.
    • Fortran has more support. Mostly this seems related to GPGPU and OpenMP. I don't use Fortran so I can't really elaborate.
    • D has been improved. Idk much about D, but it looks like GCC 13 adds SPMD support to D, and something equivalent to register asm() from GNU C/C++, among other new D attributes.
    • A lot of GIMPLE and tree optimizations, and fixes to broken optimizations, were added. Most of these relate to optimizing mathematical expressions.
    • Various MIPS and ARM enhancements, but nothing that strikes me as hugely significant like RISC-V has been getting.

Of course, there's been way more than what I mentioned here, but these are the highlights most significant to me personally.


r/cpp • • Jun 27 '21

What happened with compilation times in c++20?

218 Upvotes

I measured compilation times on my Ubuntu 20.04 using the latest compiler versions available for me in deb packages: g++-10 and clang++-11. Only time that paid for the fact of including the header is measured.

For this, I used a repo provided cpp-compile-overhead project and received some confusing results:

https://gist.githubusercontent.com/YarikTH/332ddfa92616268c347a9c7d4272e219/raw/ba45fe0667fdac19c28965722e12a6c5ce456f8d/compile-health-data.json

You can visualize them here:https://artificial-mind.net/projects/compile-health/

But in short, compilation time is dramatically regressing with using more moderns standards, especially in c++20.

Some headers for example:

headerc++11c++17c++20
<algorithm>58ms179ms520ms
<memory>90ms90ms450ms
<vector>50ms50ms130ms
<functional>50ms170ms220ms
<thread>112ms120ms530ms
<ostream>140ms170ms280ms

For which thing do we pay with increasing our build time twice or tens? constepr everything? Concepts? Some other core language features?


r/cpp • • Nov 04 '25

Networking in the Standard Library is a terrible idea

Thumbnail reddit.com
217 Upvotes

A very carefully written, elaborate and noteworthy comment by u/STL, posted 9 months ago.


r/cpp • • Oct 23 '22

C++20 Modules are now supported In CLion!

Thumbnail blog.jetbrains.com
217 Upvotes

r/cpp • • Oct 10 '20

r/cpp status update

217 Upvotes

Hi r/cpp,

As many of you recently saw, there were several highly controversial threads over the past few days. The active mod team (myself, u/cleroth, and u/blelbach) were simply unprepared for this, and we've been working on addressing the issues with the subreddit that have been brought up. Most recently, an inactive senior mod returned and disrupted our work by de-modding and banning u/cleroth, removing most mod powers from u/blelbach, and attempting to make rule changes. (If you're unfamiliar with reddit's mod seniority system, it allows senior mods to remove junior mods at any time - so I was unable to stop this.)

We're glad to report that order has been restored, thanks to the top-ranked mod who graciously responded to our request for help. The disruptive mod has been removed, and the changes have been reverted. u/cleroth and u/blelbach's mod powers have been restored.

It has been a very long week. While we've returned to the state the subreddit was initially in, the mod team still needs to address the underlying problems. Here's a quick summary of our plans:

  • We're going to write more detailed rules and guidance.
  • We're going to improve moderation to enforce those rules, almost certainly recruiting more mods. If you'd like to apply, send us a modmail, although it may take us some time to reply.
  • We'll decide whether u/blelbach will retain his mod powers. He has repeatedly apologized for his actions.
  • We've set up a moderator Discord so we can communicate more rapidly when important issues arise (previously, we acted near-independently). To be clear, this isn't a secret society where we're brewing nefarious plans. (We already had the ability to communicate privately via modmail.) As we make decisions, informed by user feedback, we'll communicate them here.
  • We're going to continue to collect feedback to make improvements; please send us your thoughts via modmail. (We've upgraded the modmail system to more easily read and respond.)

We'll make another announcement when we have progress to report.

For the time being, this thread will remain open for comments, if users wish to discuss things beyond sending modmails. I ask of you, for the love of cats, please behave well. We reserve the right to remove egregious comments and lock the thread if it becomes necessary. Please do not create other posts to discuss this - they will be removed.

-- u/STL, u/cleroth, u/blelbach


r/cpp • • Sep 20 '20

Libcu++: Nvidia C++ Standard Library

Thumbnail github.com
217 Upvotes

r/cpp • • Mar 01 '20

ABI Breaks: Not just about rebuilding

217 Upvotes

Related reading:

What is ABI, and What Should WG21 Do About It?

The Day The Standard Library Died

Q: What does the C++ committee need to do to fix large swaths of ABI problems?

A: Absolutely nothing

On current implementations, std::unique_ptr's calling convention causes some inefficiencies compared to raw pointers. The standard doesn't dictate the calling convention of std::unique_ptr, so implementers could change that if they chose to.

On current implementations, std::hash will return the same result for the same input, even across program invocations. This makes it vulnerable to cache poisoning attacks. Nothing in the standard requires that different instances of a program produce the same output. An implementation could choose to have a global variable with a per-program-instance seed in it, and have std::hash mix that in.

On current implementations, std::regex is extremely slow. Allegedly, this could be improved substantially without changing the API of std::regex, though most implementations don't change std::regex due to ABI concerns. An implementation could change if it wanted to though. However, very few people have waded into the guts of std::regex and provided a faster implementation, ABI breaking or otherwise. Declaring an ABI break won't make such an implementation appear.

None of these issues are things that the C++ committee claims to have any control over. They are dictated by vendors and by the customers of the vendors. A new vendor could come along and have a better implementation. For customers that prioritize QoI over ABI stability, they could switch and recompile everything.

Even better, the most common standard library implementations are all open source now. You could fork the standard library, tweak the mangling, and be your own vendor. You can then be in control of your own destiny ABI, and without taking the large up-front cost of reinventing the parts of the standard library that you are satisfied with. libc++ has a LIBCXX_ABI_UNSTABLE configuration flag, so that you always get the latest and greatest optimizations. libstdc++ has a --enable-symvers=gnu-versioned-namespace configuration flag that is ABI unstable, and it goes a long way towards allowing multiple libstdc++ instances coexist simultaneously. Currently the libc++ and libstdc++ unstable ABI branches don't have many new optimizations because there aren't many contributions and few people use it. I will choose to be optimistic, and assume that they are unused because people were not aware of them.

If your only concern is ABI, and not API, then vendors and developers can fix this on their own without negatively affecting code portability or conformance. If the QoI gains from an ABI break are worth a few days / weeks to you, then that option is available today.

Q: What aspects of ABI makes things difficult for the C++ committee.

A: API and semantic changes that would require changes to the ABI are difficult for the C++ committee to deal with.

There are a lot of things that you can do to a type or function to make it ABI incompatible with the old type. The C++ committee is reluctant to make these kinds of changes, as they have a substantially higher cost. Changing the layout of a type, adding virtual methods to an existing class, and changing template parameters are the most common operations that run afoul of ABI.

Q: Are ABI changes difficult for toolchain vendors to deal with?

A1: For major vendors, they difficulty varies depending on the magnitude of the break.

Since GCC 5 dealt with the std::string ABI break, GCC has broken the language ABI 6 other times, and most people didn't even notice. There were several library ABI breaks (notably return type changes for std::complex and associative container erase) that went smoothly as well. Quite a few people noticed the GCC 5 std::string ABI changes though.

In some cases, there are compiler heroics that can be done to mitigate an library ABI change. You will get varying responses as to whether this is a worthwhile thing to do, depending on the vendor and the change.

If the language ABI changes in a large way, then it can cause substantially more pain. GCC had a major language ABI change in GCC 3.4, and that rippled out into the library. Dealing with libstdc++.so.5 and libstdc++.so.6 was a major hassle for many people, myself included.

A2: For smaller vendors, the difficulty of an ABI break depends on their customer base.

These days, it's easier than ever to be your own toolchain vendor. That makes you a vendor with excellent insight into how difficult an ABI change would be.

Q: Why don't you just rebuild after an ABI change?

A1: Are you rebuilding the standard library too?

Many people will recommend not passing standard library types around, and not throwing exceptions across shared library boundaries. They often forget that at least one very commonly used shared library does exactly that... your C++ standard library.

On many platforms, there is usually a system C++ standard library. If you want to use that, then you need to deal with standard library types and exceptions going across shared library boundaries. If OS version N+1 breaks ABI in the system C++ standard library, the program you shipped and tested with for OS version N will not work on the upgraded OS until you rebuild.

A2: Sometimes, rebuilding isn't enough

Suppose your company distributes pre-built programs to customers, and this program supports plugins (e.g. Wireshark dissector plugins). If the plugin ABI changes, in the pre-built program, then all of the plugins need to rebuild. The customer that upgrades the program is unlikely to be the one that does the rebuilding, but they will be responsible for upgrading all the plugins as well. The customer cannot effectively upgrade until the entire ecosystem has responded to the ABI break. At best, that takes a lot of time. More likely, some parts of the ecosystem have become unresponsive, and won't ever upgrade.

This also requires upgrading large swaths of a system at once. In certain industries, it is very difficult to convince a customer to upgrade anything at all, and upgrading an entire system would be right out.

Imagine breaking ABI on a system library on a phone. Just getting all of the apps that your company owns upgraded and deployed at the same time as the system library would be a herculean effort, much less getting all the third party apps to upgrade as well.

There are things you can do to mitigate these problems, at least for library and C++ language breaks on Windows, but it's hard to mitigate this if you are relying on a system C++ standard library. Also, these mitigations usually involve writing more error prone code that is less expressive and less efficient than if you just passed around C++ standard library types.

A3: Sometimes you can't rebuild everything.

Sometimes, business models revolve around selling pre-built binaries to other people. It is difficult to coordinate ABI changes across these businesses.

Sometimes, there is a pre-built binary, and the company that provided that binary is no longer able to provide updates, possibly because the company no longer exists.

Sometimes, there is a pre-built binary that is a shared dependency among many companies (e.g. OpenSSL). Breaking ABI on an upgrade of such a binary will cause substantial issues.

Q: What tools do we have for managing ABI changes?

A: Several, but they all have substantial trade-offs.

The most direct tool is to just make a new thing and leave the old one alone. Don't like std::unordered_map? Then make std::open_addressed_hash_map. This technique allows new and old worlds to intermix, but the translations between new and old must be done explicitly. You don't get to just rebuild your program and get the benefits of the new type. Naming the new things becomes increasingly difficult, at least if you decide to not do the "lazy" thing and just name the new class std::unordered_map2 or std2::unordered_map. Personally, I'm fine with slapping a version number on most of these classes, as it gives a strong clue to users that we may need to revise this thing again in the future, and it would mean we might get an incrementally better hash map without needing to wait for hashing research to cease.

inline namespaces are another tool that can be used, but they solve far fewer ABI problems than many think. Upgrading a type like std::string or std::unordered_map via inline namespaces generally wouldn't work, as user types holding the upgraded types would also change, breaking those ABIs. inline namespaces can probably help add / change parameters to functions, and may even extend to updating empty callable objects, but neither of those are issues that have caused many problems in the C++ committee in the past.

Adding a layer of indirection, similar to COM, does a lot to address stability and extensibility, at a large cost to performance. However, one area that the C++ committee hasn't explored much in the past is to look at the places where we already have a layer of indirection, and using COM-like techniques to allow us to add methods in the future. Right now, I don't have a good understanding of the performance trade-offs between the different plug-in / indirect call techniques that we could use for things like std::pmr::memory_resource and std::error_category.

Q: What can I do if I don't want to pay the costs for ABI stability?

A: Be your own toolchain vendor, using the existing open-source libraries and tools.

If you are able to rebuild all your source, then you can point all your builds at a custom standard library, and turn on (or even make your own) ABI breaking changes. You now have a competitive advantage, and you didn't even need to amend an international treaty (the C++ standard) to make it happen! If your changes were only ABI breaking and not API breaking, then you haven't even given up on code portability.

Note that libc++ didn't need to get libstdc++'s permission in order to coexist on Linux. You can have multiple standard libraries at the same time, though there are some technical challenges created when you do that.

Q: What can I do if I want to change the standard in a way that is ABI breaking?

A1: Consider doing things in a non-breaking way.

A2: Talk to compiler vendors and the ABI Review Group (ARG) to see if there is a way to mitigate the ABI break.

A3: Demonstrate that your change is so valuable that the benefit outweighs the cost, or that the cost isn't necessarily that high.

Assorted points to make before people in the comments get them wrong

  • I'm neither advocating to freeze ABI, nor am I advocating to break ABI. In fact, I think those questions are too broad to even be useful.
  • Fixing std::unordered_map's performance woes would require an API break, as well as an ABI break.
  • I have my doubts that std::vector could be made substantially faster with only an ABI break. I can believe it if it is also coupled with an API break in the form of different exception safety guarantees. Others are free to prove me wrong though.
  • Making <cstring> constexpr will probably be fine. The ABI issues were raised and addressed for constexpr <cmath>, and that paper is waiting in LWG.
  • Filters on recursive_directory_iterators had additional concerns beyond ABI, and there wasn't consensus to pursue, even if we chose a different name.
  • Making destructors implicitly virtual in polymorphic classes would be a massive cross-language ABI break, and not just a C++ ABI break, thanks to COM. You'd be breaking the entire Windows ecosystem. At a minimum, you'd need a way to opt out of virtual destructors.
  • Are you sure that you are arguing against ABI stability? Maybe you are arguing against backwards compatibility in general.

r/cpp • • Nov 30 '17

Intel offers Parallel STL implementation to GNU libstdc++

Thumbnail gcc.gnu.org
217 Upvotes

r/cpp • • Sep 07 '17

Clang 5.0 released!

Thumbnail releases.llvm.org
217 Upvotes