Questions tagged [constant-expression]

Constant expressions can be evaluated at compile time.

Many languages require certain initializers to be constant expressions. For example:

  • Array bounds
  • Selectors in case statements
  • Bit-field length specification
  • Enumeration initializers
  • Non-type template arguments

A greatly restricted set of operands are allowed in constant expressions. Generally, variables are not allowed. For example, C++ allows:

  • Literals
  • Enumeration constants
  • Values declared as const that are initialized with constant expressions
  • sizeof expressions

Questions with this tag usually need help with error messages that indicate lines that require constant expressions.

215 questions
0
votes
0 answers

Why I can't have a private static field with Object value in php?

Sorry but I have had this problem for months and I didn't find any answer for it. Here is my code:
0
votes
0 answers

C# Idiomatic runtime expression which always evaluates to true but is not detected as compile-time constant

I would like to have a C# runtime expression which always evaluates to true, but which the compiler does not recognize as a constant. (Motivation below) There are plenty of ways to achieve this, but I'm wondering if there's a nice most-idiomatic…
0
votes
1 answer

Using Reinterpret_Cast in a Constexpr Function

To my understanding C++11 specifically designates that reinterpret_cast cannot be used within a constant expression. The reason (again to my understanding) is that the compiler cannot interpret the validity of the conversion. With that being said,…
Ryoku
  • 397
  • 2
  • 16
0
votes
3 answers

Why static definition of variable in C fails while compiling?

please could anyone help me why such a definition of symbolic constants yields error definition of following static variables within functions: error: storage size of ‘variable’ isn’t constant #define SAMPLE_RATE 200 /* Sample rate in Hz.…
0
votes
1 answer

Expression, Constant List, Compiler generated calss

I have this simple code: public void MyWhere( Expression> predicate) { } List Indexes2 = new List(); Indexes2.Add("abc"); MyWhere(a=>Index2.Contains(a.a1)); While parsing the expression, that Index2 appears as a…
0
votes
2 answers

C error: variable-sized object may not be initialized

I'm currently learning C language from youtube and this is one of the code about the 2D arrays: #include int main() { int const columns = 3; int const rows = 2; int grades[rows][columns] = { {12, 23, 45}, {64,…
0
votes
1 answer

Creating lambda expression ConstantExpression with a string value

I want to create lambda expression providing property name, a value (as string) and property type (as Type). The problem with that is in line Expression.Constant(value1, propertyType); value1 that is passed to Foo is string. and must be parsed to…
jullin
  • 633
  • 2
  • 12
  • 21
0
votes
1 answer

take template basis on run-time value

I have an enum: enum operation { plus, delete //... } There is function, which has a run-time argument. operation_do(plus); And inside that function, there is a template-function call, which is based on a run-time argument. Important: I can NOT…
Nikita Gusev
  • 143
  • 10
0
votes
0 answers

C11, 6.6.10: IB: other forms of constant expressions: additional conformance documentation is needed

Why it (seems that it) is a general practice for C compiler vendors to not provide to the end users an additional conformance documentation about implementation-defined behavior regarding «other forms of constant expressions» (C11, 6.6.10)? C11,…
0
votes
2 answers

What types of objects can constexpr pointers point to?

cppreference.com says: A constexpr specifier used in an object declaration implies const. But I tried to make a constexpr pointer hold the address of a const object of the same base-type but the compiler gave me an error: const int a = 1; int…
Mason
  • 501
  • 3
  • 11
0
votes
2 answers

Can initialized struct members not serve as integer constants in C?

I have declared and initialized a struct at the top of the file like so: struct myDataTypes { int INT; int DOUBLE; int FLOAT; } types = {0,1,2}; When I try to use types.INT in a case of a switch, I get the compiler error that the case…
Theo d'Or
  • 783
  • 1
  • 4
  • 17
0
votes
3 answers

SQL Query: A constant expression was encountered in the ORDER BY list

I'm getting the following error: A constant expression was encountered in the ORDER BY list, position 1. I'm trying to order by cell values in a column. The query is three union select statements. Code below: Declare @RefDate date =…
Matt
  • 9
  • 3
0
votes
1 answer

Why am I suddenly getting "Compile Error: Constant Expression Required"?

I have the following code: Private Enum dbi m = 0 d = 1 y = 2 hh = 3 mm = 4 ss = 5 ap = 6 size = 7 End Enum ... Sub CompileData() ... ReDim startEnd(deplCount * 2) As Date …
0
votes
2 answers

Java - Switch Label with parentheses

I found that parentheses can be used in switch label, e.g.: switch(id) { case (CONSTANT): case (1): // Do action break; } But why Java allow parentheses in this case, is there a use case ? because I can't use || or , to use multiple,…
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0
votes
0 answers

Do Java compilers calculate constant expressions during the compilation?

I've noticed that enum values in java.util.concurrent.TimeUnit have methods something like: public long toMillis(long d) { return x(d, C3/C2, MAX/(C3/C2)); } Are expressions C3/C2 and MAX/(C3/C2) calculated during compilation or during runtime?