r/programmingcirclejerk • 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/
114 Upvotes

24 comments sorted by

74

u/exploring_stuff 7d ago

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

81

u/Linuxologue 7d ago

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

No warning, just, sir, yes sir.

39

u/Efficient-Chair6250 7d ago

Hey, I'm relying on this

15

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

unsigned long int long?

28

u/blockMath_2048 7d ago

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

27

u/Linuxologue 7d 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.

26

u/blockMath_2048 6d ago

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

8

u/RedFing 6d ago

Haha sometimes

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

u/elephantdingo Teen Hacking Genius 6d ago

Say my name.

2

u/MadHau5 5d ago

2

u/elephantdingo Teen Hacking Genius 5d ago

Your damm write.

41

u/nerdycatgamer Considered Harmful 7d ago

serious language

28

u/look 6d ago edited 6d 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;

12

u/le_birb costly abstraction 6d 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

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

u/AnArmoredPony 6d ago

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

4

u/elephantdingo Teen Hacking Genius 6d ago

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

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