Home > On-Demand Archives > Q&A Sessions >

Live Q&A - Compile-Time Programming in C++ Using Constexpr

Ben Saks - Watch Now - EOC 2023 - Duration: 34:59

Live Q&A - Compile-Time Programming in C++ Using Constexpr
Ben Saks
Live Q&A with Ben Saks for the talk titled Compile-Time Programming in C++ Using Constexpr
M↓ MARKDOWN HELP
italicssurround text with
*asterisks*
boldsurround text with
**two asterisks**
hyperlink
[hyperlink](https://example.com)
or just a bare URL
code
surround text with
`backticks`
strikethroughsurround text with
~~two tilde characters~~
quote
prefix with
>

Nathan3
Score: 1 | 2 years ago | 1 reply

Thanks Ben for the talk. I think I have seen that constexpr is coming to C23 for variables. Could you explain a bit here how it compare to current const keyword in C and to constexpr in C++11 and later ?

BenSaksSpeaker
Score: 1 | 2 years ago | no reply

Hi Nathan3, glad you enjoyed the talk.
Compiler support for the C23 version of constexpr seems to be fairly new, but it appears that the C23 version of constexpr will be a strict subset of the constexpr features available in C++. C23 will permit some kinds of constexpr objects, but not constexpr functions. As such, some existing C++ code will be valid C23 code, making life easier for those who need their code to compile as either C or C++. We'll also be able to use constexpr objects where today we would use macros; that will be nice because (unlike evaluating a macro), evaluating a constexpr object can't produce side effects.
Unlike const objects, constexpr objects will be considered constant expressions in both C and C++. As such, you'll be able to use constexpr objects in contexts that require constant expressions, such as enum definitions:
int const cx1 = 10;
enum { e1 = cx1 }; // valid in C++, but not in C

 constexpr int cx2 = 20;
 enum { e2 = cx2 };    // valid in C++ and C23

 Hope that helps - let me know if you have additional questions.

OUR SPONSORS

OUR PARTNERS