r/cpp_questions 5d ago

OPEN What is the most impressive compile time C++ code you've seen

I've recently been laid off so I have been spending a lot more time writing code I want to write. Lately I've taken an interest in actually learning TMP which is something I've been wanting to do, but until now, I've mostly used templates for generic data structures.

So rather than go find videos, tutorials, or traditional learning materials, I decided I would build and optimize a 3DGS library using C++23 and TMP while using AI as a learning tool. Currently, I have built and optimized a forward pass renderer to frame times comparabable with the best publicly available tools.

In doing so, I have implemented the following compile time features;

- Static arena memory layout calculations (both on the gpu and cpu)

- Perfectly inlined and unrolled render graph execution with conditional branching. I'd like to implement some form of concurrency also.

- Optimized wrappers around Vulkan Compute types, as many of these values are known at compile time.

- Currently working on an abstraction between the interface and the different backends so they are somewhat interoperable.

The thing is, sometimes I'm not so sure what the LLM is suggesting is the best approach. I will push back and sometimes it gives in. But I can't tell if that's my traditional c++ mindset fighting the process, or if the AI is just not up to date.

So I'm in search of exceptionally written codebases to study. Things with a heavy reliance on C++20/23 and compile time optimizations. What projects come to mind?

46 Upvotes

18 comments sorted by

28

u/light_switchy 5d ago

I dunno if I'd call it "impressive", but Boost Spirit was pretty crazy given the state of the art at the time it was written. Joel De Guzman is the guy behind it, if I remember right. Definitely pushed the boundaries at the time.

23

u/mredding 5d ago

I always thought dimensional analysis libraries were so elegant. They solve for units all at compile time, give you access to that dimensionality, and fail to compile if you mix units. And for all the templating, it all boils off from the generated machine code.

They say comstexpr combined with reflection have rendered TMP outmoded, so I'd be curious to see such a library written this way.

2

u/MiamiGunworks 5d ago

I thought constexpr didn't even get the job it was supposed to done, which is why consteval was needed.

I haven't looked at reflection yet, but so far I've been able to do most anything I want with templates. I don't really see a reason for reflection.

1

u/TREE_sequence 4d ago

Reflection lets you introspect members of class types, the names and values of enum constants, and even the width of bit fields, plus allows promoting containers with dynamic allocations built at compile-time into static storage to allow using them in in constant expressions — combined with the “template for” expansion statement, that last one alone can enable doing things like making an itoa function that’s eight times as general in half the code (using supported integer types as a measure of generality), and with flat_map and inplace_vector it allows making an enum-to-string map a constant expression with zero preprocessor shenanigans or startup code required. It’s huge, and anyone who thinks otherwise hasn’t seen what it can do yet

1

u/Potterrrrrrrr 3d ago

I still use gperf to generate perfect hashes for my strings, it’ll be interesting to see how much that can be replaced with reflection, whenever MSVC gets around to it at least

15

u/Secoupoire 5d ago

Hana “Ms. Constexpr” Dusíková (as Herb Sutter calls her) work on TMP is a reference. For instance her work on compile-time regex. https://github.com/hanickadot/compile-time-regular-expressions

3

u/javascript 4d ago

Her work on the library itself was amazing. The fact that she wrote it a second time in Javascript for interactive tutorials was the cherry on top!

5

u/frasnian 5d ago

With the relaxations on constexpr, it's very rare that have to I fall back on TMP anymore. Obviously the syntax is much more natural, it's much more flexible, and using it in certain ways can force compile-time evaluation (even if it can be run-time in other contexts). Combined with concepts, it's pretty great.

That being said, sometimes I still find TMP necessary or convenient. It's just no longer the only tool in the toolbox, or (IMO) the best one in most cases.

Hell, I'll still occasionally use #define as a code generator for boilerplate sections.

1

u/TREE_sequence 4d ago

Not me using preprocessor macros combined with reflection metafunctions and splices to generate the same string (or really character array, to generate constants for token lexing) in all standard encodings because there’s still no way to metaprogram string literal operators/prefixes

2

u/wrd83 5d ago

Boost hana was pretty cool stuff.

3

u/centuryx476 5d ago

Why you get laid off??

Ai ?

Outsourced??

8

u/MiamiGunworks 5d ago

Failing startup.

2

u/keelanstuart 5d ago

I've got a collection of libraries that I've been working on for the last couple of decades for my personal projects. I've updated them to work with newer C++ standards and, with a few, ported them to Linux.

https://www.github.com/keelanstuart

There's been an urgency over the last year, or so, to get things spiffed up, "just in case". The only thing I really used any AI for was generating some of the readme content (which I then edited). I hate allocating memory willy-nilly. I prefer explicit lifetimes. If you're bored, have a look at some of my projects... if you're not bored, that's also fine.

2

u/Conscious-Pick9518 4d ago

I recently made a wasm interpreter in constexpr and ran doom ;)

https://github.com/Talkative-Banana/Constexpr-Doom

2

u/drex_vke 4d ago

hi i view your code. Hand there's no point in putting std::string_view in passing by reference in handler.hpp more precisely the Handlecall function :)

2

u/Conscious-Pick9518 4d ago

Lol you are right idk what was I thinking :D

1

u/drex_vke 4d ago

ha ha no big deal because it has already happened to me too :)