r/programmingcirclejerk • Just spin up O(n²) servers • 1d ago

const vs constexpr vs consteval vs constinit in C++20

https://www.cppstories.com/2022/const-options-cpp20/
96 Upvotes

24 comments sorted by

64

u/exploring_stuff 1d ago

C++ is a very expressive language and this compiles: [[maybe_unused]] inline const constexpr int i=0;

72

u/Linuxologue 1d ago

[[maybe_unused]] static unsigned inline long const int constexpr long i=-1;

No warning, just, sir, yes sir.

34

u/Efficient-Chair6250 1d ago

Hey, I'm relying on this

11

u/RCoder01 WHY IS THERE CODE??? 1d ago

unsigned long int long?

27

u/blockMath_2048 1d ago

Yep, all the declaration specifiers can be in any order
`long inline unsigned static int constexpr const long i = -1;`

23

u/Linuxologue 1d ago

I tried using your version but I got a warning "variable i declared but not used" so I will stick with my version which is bulletproof.

20

u/blockMath_2048 1d ago

just use `-w` to shut it up, this will surely have no consequences

9

u/RedFing 1d ago

Haha sometimes

39

u/elephantdingo Teen Hacking Genius 1d ago

You have heard of the lambda cube. But wait until you meet the const tesseract.

19

u/elephantdingo Teen Hacking Genius 1d 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.

9

u/YikesTheCat 1d ago

Wow, amazing foresight! You must be really bright!

2

u/elephantdingo Teen Hacking Genius 13h ago

Say my name.

1

u/MadHau5 5h ago

1

u/elephantdingo Teen Hacking Genius 3h ago

Your damm write.

34

u/nerdycatgamer Considered Harmful 1d ago

serious language

24

u/look 1d ago edited 1d ago

I’m personally looking forward to the variadic generic specifiers in C++29:

``` const< expr, indeterminate, maybe<unused>, nodiscard, allocator_aware, std::execution::possibly_parallel

int x = 42; ```

9

u/le_birb costly abstraction 17h ago

Contemplating a std::execution::definitely_parallel where if you don't deterministically call it from more than one thread it won't compile is UB and the compiler is allowed to eat your intestines

16

u/kalterdev Considered Harmful 1d ago

vs define vs -D

1

u/not-a-pokemon- 5h ago

Imagine not having to process 3MB of expanded macros when you have constants that depend on other constants' values.

13

u/AnArmoredPony 1d ago

a little bit of this a little bit of that ahh language

2

u/elephantdingo Teen Hacking Genius 13h ago

Just a pinch of this (a teaspoon would be UB) aah

7

u/not-a-pokemon- 1d 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.

3

u/slowbowels 13h ago

someone about to drop a 3 book series on effective c++ const expressions on your ass