C++ Weekly - Ep 261 - C++20's New consteval Keyword

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

It would have been interesting to mention that one of the main motivations for consteval is that it is a requirement for the reflection TS, which needs that functions that take and return reflection objects (effectively AST nodes) be called at compile time, as its the only time when the AST actually exists.

asierelorz
Автор

Im all in for more ways to enforce compile-time operations :D

codingwithmat
Автор

use case: lookup table generation for e.g. a crc algorithm

m.r.
Автор

A good use case of the consteval is for regex and print format parsing.

akj
Автор

I am sooo glad to see that you were willing to eat humble pie and admit to all of us that you made a boo boo in forgetting to make an episode on consteval. If you were a politician, you would have been scrambling to find someone else to blame for this oversight.

GregoryTheGrster
Автор

Thank you Jason for this insightful episod about consteval.

danielphd
Автор

Now, we wait patiently for reflexpr...

bettkitty
Автор

A very great use case for consteval is user-defined string literals, that should be checked for syntactic correctness at compile time. For example, for our company's code base I developed such consteval string literals for IPs, e.g.: "173.56.134.2"_ip
(More complicated it gets for IPv6 string literals.)

Deniz.Bahadir
Автор

Could we get one on constinit as well? To complete the set.

davidkatz
Автор

I think consteval is also useful to have compile time inlining of code/data when compiling code with low optimizer flags -O0 vs -O3.
Compiler explorer just confirmed this.

maartenofbelgium
Автор

An excellent use case for consteval are functions/classes for creating compile-time-known bit-masks (eg for writing or checking registers). Something missing from this video is that when a function is evaluated at compile time there is a requirement that undefined behaviour be diagnosed, whereas a constexpr function that *could* be evaluated at compile time does not, unless forced to (for example, assigning the result to a constexpr variable). Even if the compiler then takes advantage of the constexpr function with compile-time-known parameters and optimises it out, it doesn't require a diagnostic! A bit-shift that goes outside of the bounds of the type is UB, but will only be detected if you ensure that it's assigned to a constexpr variable. A preprocessor macro actually ends up being safer, as it will usually throw a warning. With consteval you can forget that song and dance and just create your compile time constant mask however you like, knowing the compiler will catch your worst mistakes.

TimSmaill
Автор

I have A TON of uses for consteval functions in my heavily templated code.

VioletGiraffe
Автор

Please do `constinit`, with explanation of what `static initialization order fiasco` really means

ivanzvonimirhorvat
Автор

This could be great for parsing. Like forcing a class to add pointers to its members to a static list. I don't know how yet, but I'm intrigued.

OperationDarkside
Автор

can I overload on such a function declaration with a non consteval?

eLBehmo
Автор

If the input parameter of the `consteval` function is required to be known at compile time, why cannot we use it as a template argument inside of this `consteval` function?

consteval int getValue(const int x) { return foo<x>(); }

ProXicT
Автор

consteval is great but using enum is finally here in gcc 11. FINALLY my pain is to be lessnd.

therealchonk
Автор

So the question is, can we PLEASE get "for consteval(...)" now?

simonfarre
Автор

consteval has to be able to be evaluated at compile time but is it guaranteed to actually be evaluated at compile time?

acf
Автор

Should user-defined literals be consteval?

MalcolmParsons