Most examples I found on the web preferred the "west style" for constexpr
(C++11), consteval
, and constinit
(C++20):
consteval auto sqr(int n) {
return n*n;
}
constexpr auto r = sqr(100); // OK
constinit static auto x = r;
Since I`m not a language lawyer, I have the following question: Is the "east style" allowed for these specifiers? Example:
auto consteval sqr(int n) {
return n*n;
}
auto constexpr r = sqr(100); // OK
static auto constinit x = r;
To be clear: I do not intent to start a "west" / "east" language war. I'm not interested in opinions, just in facts, especially as clang and gcc head versions on wandbox at this moment give errors but no answers on constinit
/ consteval
.
In contrast to a constexpr function, a consteval function can only be executed at compile time. consteval creates a so-called immediate function. Each invocation of an immediate function creates a compile-time constant. consteval cannot be applied to destructors or functions that allocate or deallocate.
We use the consteval specifier in C++ to declare an immediate function. An immediate function is a function that must be evaluated at the compile-time to produce a constant. In other words, an immediate function is executed at compile-time.
constinit - asserts that a variable has static initialization, i.e. zero initialization and constant initialization, otherwise the program is ill-formed.
Sort of.
It's part of the decl-specifier-seq, and the specifiers in that can be in any order. It's the same rule that allows you to write volatile int static long unsigned inline long const x = 1;
But it isn't part of the declarator (in particular, the ptr-operator), so you can't do int* constexpr x = nullptr;
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With