Message from C, C++ talks

July 2019

— Because you didn't mark the function as constexpr

— Constexpr implies const

constexpr int f{ 3 }; // f has type const int

— Now list all the conditions a func must meet to may be evaluated at compile time

— It may be harder than virtual functions


You can find them in cppreference. In C+11 were restricted to a single return statement, but they relaxed that in C++14. You can do pretty much anything that doesn't involve dynamic allocations

— Remember, we're talking about novices: people, who hasn't even heard about classes e.g. They're struggling with unformatted text output

Message permanent page

— It's as simple as "constexpr implies compile time execution instead of runtime"

— If you have constexpr int sum(int lhs, int rhs){ return lhs + rhs; }, what you're telling the compiler is "Hey, do this at compile time if you can"

Message permanent page

— In C++20 we will have inmediate functions, which will be marked as consteval. This forces the function to be executed at compile time

Message permanent page

— First bullet:
> its return type must be a LiteralType
Okay. Let's take a look
> possibly cv-qualified (da hell?) void
Well, that's relatively easy to google
> trivial dtor, aggregate, closure
Et cetera

Message permanent page

— One need to know hella lot terms and principles to understand conxtexpr conditions

— It may sound easy for one who faced this things a lot

— I don't even understand all of them. I just program functions normally, mark them as contexpr and wait for the compiler to tell me what's wrong and adapt as I go

Message permanent page

— Just avoid dynamic allocations and you're good to go

— They're not used for everything, but if you think your function is quite simple, just mark it as constexpr and the compiler will execute at compile time everytime it can

Message permanent page