constexpr not compiling in VC2013
Microsoft publishes a C++11 compatibility table, under which constexpr is clearly marked as not being available in Visual Studio 2013. The November 2013 CTP has it, though. Source: Google visual studio constexpr
Microsoft publishes a C++11 compatibility table, under which constexpr is clearly marked as not being available in Visual Studio 2013. The November 2013 CTP has it, though. Source: Google visual studio constexpr
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
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
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
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
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
A class is allowed to have a static member of the same type. However, a class is incomplete until the end of its definition, and an object cannot be defined with incomplete type. You can declare an object with incomplete type, and define it later where it is complete (outside the class). struct Size { … Read more
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.
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
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