How can the compile-time be (exponentially) faster than run-time?

GCC is likely memoizing constexpr functions (enabling a Θ(n) computation of fib(n)). That is safe for the compiler to do because constexpr functions are purely functional. Compare the Θ(n) “compiler algorithm” (using memoization) to your Θ(φn) run time algorithm (where φ is the golden ratio) and suddenly it makes perfect sense that the compiler is … Read more

static constexpr member of same type as class being defined

If I interpret the Standard correctly, it isn’t possible. (§9.4.2/3) […] A static data member of literal type can be declared in the class definition with the constexpr specifier; if so, its declaration shall specify a brace-or-equal-initializer in which every initializer-clause that is an assignment-expression is a constant expression. […] From the above (along with … Read more

Does specifying constexpr on a constructor automatically make all objects created from it to be constexpr?

Having a constexpr constructor does not make declarations of that variable automatically constexpr, so t is not a constexpr. What is going on in this case is that you are calling a constexpr function, this line: constexpr int b = t+test(); can be viewed as follows: constexpr int b = t.operator+( test() ); So then … Read more

Use of constexpr in header file

constexpr implies const and const on global/namespace scope implies static (internal linkage), which means that every translation unit including this header gets its own copy of PI. The memory for that static is only going to be allocated if an address or reference to it is taken, and the address is going to be different … Read more

Why do constant expressions have an exclusion for undefined behavior?

The wording is actually the subject of defect report #1313 which says: The requirements for constant expressions do not currently, but should, exclude expressions that have undefined behavior, such as pointer arithmetic when the pointers do not point to elements of the same array. The resolution being the current wording we have now, so this … Read more

How does the C++ compiler evaluate recursive constexpr functions so quickly?

constexpr functions have no side-effects and can thus be memoized without worry. Given the disparity in runtime the simplest explanation is that the compiler memoizes constexpr functions during compile-time. This means that fibonacci(n) is only computed once for each n, and all other recursive calls get returned from a lookup table.

Why do we need to mark functions as constexpr?

Preventing client code expecting more than you’re promising Say I’m writing a library and have a function in there that currently returns a constant: awesome_lib.hpp: inline int f() { return 4; } If constexpr wasn’t required, you – as the author of client code – might go away and do something like this: client_app.cpp: #include … Read more

Why is C++11 constexpr so restrictive?

The reason you’d need to write statements instead of expressions is that you want to take advantage of the additional capabilities of statements, particularly the ability to loop. But to be useful, that would require the ability to declare variables (also banned). If you combine a facility for looping, with mutable variables, with logical branching … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)