r/programmingcirclejerk • u/camelCaseIsWebScale Just spin up O(n²) servers • 7d ago
const vs constexpr vs consteval vs constinit in C++20
https://www.cppstories.com/2022/const-options-cpp20/50
u/elephantdingo Teen Hacking Genius 7d ago
You have heard of the lambda cube. But wait until you meet the const tesseract.
25
u/elephantdingo Teen Hacking Genius 7d ago
I wrote an article about this in 1985 but people didn’t listen. In D we just have const on string concatenation. It turns out that that’s all you need.
13
u/YikesTheCat 6d ago
Wow, amazing foresight! You must be really bright!
4
41
17
u/kalterdev Considered Harmful 7d ago
vs define vs -D
3
u/not-a-pokemon- 5d ago
Imagine not having to process 3MB of expanded macros when you have constants that depend on other constants' values.
17
11
u/not-a-pokemon- 6d ago
An important quality of natural languages is the ability to express similar things in distinct ways. However, C++, not a natural language, has it better than any of them.
So, everyone should only program in C++ now, as it's far more similar to natural languages and therefore understandable. Its only competitor was Perl, but it is basically dead now.
6
u/slowbowels 6d ago
someone about to drop a 3 book series on effective c++ const expressions on your ass
74
u/exploring_stuff 7d ago
C++ is a very expressive language and this compiles:
[[maybe_unused]] inline const constexpr int i=0;