Message from C, C++ talks

July 2019

— I think constexpr is even easier to explain than plain const

— Rly? Then say what type s has in constexpr char* s{ "str" };

— And why this is a compilation error

— To a person who isn't sure how to use if


Constexpr doesn't really say anything about the type like const does. It just tells the compiler that the value has to be known at compile time

— 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

Message permanent page

— 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