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

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

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