compile-time-constant
Why isn’t std::string::max_size a compile-time constant?
One reason is that the max_size function isn’t very useful at all, and the committee doesn’t think it is worth the trouble to try to fix it. So it is just left the way it is, because it is part of the documented interface. See library defect report #197: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3440.html#197 max_size() isn’t useful for very … Read more
What’s the difference between equ and db in NASM?
The first is equate, similar to C’s: #define len 2 in that it doesn’t actually allocate any space in the final code, it simply sets the len symbol to be equal to 2. Then, when you use len later on in your source code, it’s the same as if you’re using the constant 2. The … Read more
M_PI flagged as undeclared identifier
It sounds like you’re using MS stuff, according to their docs Math Constants are not defined in Standard C/C++. To use them, you must first define _USE_MATH_DEFINES and then include cmath or math.h. So you need something like #define _USE_MATH_DEFINES #include <cmath> as a header.
Dividing by zero in a constant expression
Yes, division by zero is undefined behavior and neither the C nor C++ standard impose any requirements in such cases. Although in this case I believe you should at least issue a diagnostic(see below). Before I go quoting the standards, I should note that although this may be conformant behavior quality of implementation is a … Read more
Why isn’t a final variable always a constant expression?
From the JLS A blank final is a final variable whose declaration lacks an initializer. A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (ยง15.28). Your variable final int a; is a blank final variable. It lacks an initializer. The second paragraph doesn’t apply … Read more
Compile-time constants and variables
Compile time constant must be: declared final primitive or String initialized within declaration initialized with constant expression So private final int x = getX(); is not constant. To the second question private int y = 10; is not constant (non-final in this case), so optimizer cannot be sure that the value would not change in … Read more
constexpr overloading
I agree that this feature is missing – I need it too. Example: double pow(double x, int n) { // calculate x to the power of n return … } static inline double pow (double x, constexpr int n) { // a faster implementation is possible when n is a compile time constant return … … Read more
How to declare a constant Guid in C#?
No. The const modifier only applies to “primitive” types (bool, int, float, double, long, decimal, short, byte) and strings. Basically anything you can declare as a literal.
Java switch statement: Constant expression required, but it IS constant
I understand that the compiler needs the expression to be known at compile time to compile a switch, but why isn’t Foo.BA_ constant? While they are constant from the perspective of any code that executes after the fields have been initialized, they are not a compile time constant in the sense required by the JLS; … Read more