constant-expression

Why was the restriction on the comma operator being in a constant expression removed in C++11?

萝らか妹 提交于 2019-11-27 21:59:25
Recently when answering a question I realized that the comma operator is allowed in a constant expression in C++11 as long as the expression is surrounded by () , for example: int a[ (1, 2) ] ; Pre C++11 it is forbidden to use the comma operator in a constant expression, from the draft pre C++11 standard section 5.19 Constant expressions which says ( emphasis mine ): [...]In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used. Why was the comma

Is gcc considering builtins of non-constant expression functions to be constant expressions

北慕城南 提交于 2019-11-27 21:29:54
Please see the update for a better sample of the problem. The original code has a mix of issues which muddies the picture : This question Why can I call a non-constexpr function inside a constexpr function? presented the following code #include <stdio.h> constexpr int f() { return printf("a side effect!\n"); } int main() { char a[f()]; printf("%zd\n", sizeof a); } Which as I answer is ill-formed but gcc 4.8.2 allows it ( see it live ). But, if we use the -fno-builtin flag gcc generates an error ( see it live ): error: call to non-constexpr function 'int printf(const char*, ...)' return printf(

Why are lambda expressions not allowed in an unevaluated operands but allowed in the unevaluated portions of constant expressions?

浪子不回头ぞ 提交于 2019-11-27 04:51:19
If we look at the draft C++ standard section 5.1.2 Lambda expressions paragraph 2 says ( emphasis mine going forward ): The evaluation of a lambda-expression results in a prvalue temporary (12.2). This temporary is called the closure object. A lambda-expression shall not appear in an unevaluated operand (Clause 5). [ Note: A closure object behaves like a function object (20.8).—end note ] and section 5.19 Constant expressions paragraph 2 says: A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression (3.2), but

Example of something which is, and is not, a “Constant Expression” in C?

旧城冷巷雨未停 提交于 2019-11-27 02:54:43
问题 I'm a tad confused between what is and is not a Constant Expression in C, even after much Googleing. Could you provide an example of something which is, and which is not, a Constant Expression in C? 回答1: A constant expression can be evaluated at compile time. That means it has no variables in it. For example: 5 + 7 / 3 is a constant expression. Something like: 5 + someNumber / 3 is not, assuming someNumber is a variable (ie, not itself a compile-time constant). 回答2: There is another subtlety

Constant expression initializer for static class member of type double

天涯浪子 提交于 2019-11-27 02:08:54
In C++11 and C++14, why do I need constexpr in the following snippet: class Foo { static constexpr double X = 0.75; }; whereas this one produces a compiler error: class Foo { static const double X = 0.75; }; and (more surprisingly) this compiles without errors? class Foo { static const double X; }; const double Foo::X = 0.75; In C++03 we were only allowed to provide an in class initializer for static member variables of const integral of enumeration types, in C++11 we could initialize a static member of literal type in class using constexpr. This restriction was kept in C++11 for const

Is gcc considering builtins of non-constant expression functions to be constant expressions

大城市里の小女人 提交于 2019-11-26 23:04:05
问题 Please see the update for a better sample of the problem. The original code has a mix of issues which muddies the picture : This question Why can I call a non-constexpr function inside a constexpr function? presented the following code #include <stdio.h> constexpr int f() { return printf("a side effect!\n"); } int main() { char a[f()]; printf("%zd\n", sizeof a); } Which as I answer is ill-formed but gcc 4.8.2 allows it ( see it live ). But, if we use the -fno-builtin flag gcc generates an

Why doesn't a Java constant divided by zero produce compile time error? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-26 20:57:19
问题 Possible Duplicate: Is 1/0 a legal Java expression? Why does this code compile? class Compiles { public final static int A = 7/0; public final static int B = 10*3; public static void main(String[] args) {} } If I take a look in the compiled class file, I can see that B has been evaluated to 30, and that A still is 7/0. As far as I understand the JSL an expression where you divide by zero is not a constant. Ref: JLS 15.28 My above statement is due to this line: A compile-time constant

Why was the restriction on the comma operator being in a constant expression removed in C++11?

徘徊边缘 提交于 2019-11-26 20:52:25
问题 Recently when answering a question I realized that the comma operator is allowed in a constant expression in C++11 as long as the expression is surrounded by () , for example: int a[ (1, 2) ] ; Pre C++11 it is forbidden to use the comma operator in a constant expression, from the draft pre C++11 standard section 5.19 Constant expressions which says ( emphasis mine ): [...]In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and

C++ expected constant expression

风流意气都作罢 提交于 2019-11-26 12:46:21
问题 #include <iostream> #include <fstream> #include <cmath> #include <math.h> #include <iomanip> using std::ifstream; using namespace std; int main (void) { int count=0; float sum=0; float maximum=-1000000; float sumOfX; float sumOfY; int size; int negativeY=0; int positiveX=0; int negativeX=0; ifstream points; //the points to be imported from file //points.open( \"data.dat\"); //points>>size; //cout<<size<<endl; size=100; float x[size][2]; while (count<size) { points>>(x[count][0]); //cout<<\"x=

Constant expression initializer for static class member of type double

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 12:29:55
问题 In C++11 and C++14, why do I need constexpr in the following snippet: class Foo { static constexpr double X = 0.75; }; whereas this one produces a compiler error: class Foo { static const double X = 0.75; }; and (more surprisingly) this compiles without errors? class Foo { static const double X; }; const double Foo::X = 0.75; 回答1: In C++03 we were only allowed to provide an in class initializer for static member variables of const integral of enumeration types, in C++11 we could initialize a