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
1
vote
1 answer

Constant Expression in Linq to SQL is not correctly compiled

I have two table (mysql) with master-detail relationship that I want to query in Linq (and then experiment it in LinqPad). The problem is Linq to SQL can't produce the correct result nor SQL statement for the following query from m in masters select…
qsoft
  • 424
  • 2
  • 5
  • 17
1
vote
1 answer

Variable cannot appear in a constant-expression

I'm having a hard time figuring out why GCC 4.5 won't let me compile this: #include #include #define WIDTH 512 #define HEIGHT 512 #define CEIL_POS(X) ((X - (unsigned int)(X)) > 0 ? (unsigned int)(X + 1) : (unsigned…
Mihai Todor
  • 8,014
  • 9
  • 49
  • 86
0
votes
1 answer

Must a constant expression be evaluated to an integral type?

Lets say I have the following: int i = 1; String str("abc"); Would str be consider a constant expression? From lots of C++ books, it seems a constant expression must be evaluated to an integral type.
yapkm01
  • 3,590
  • 7
  • 37
  • 62
0
votes
0 answers

Recursively descending generic constant

Is there a way to bring this snippet to successful compilation ? #![feature(generic_const_exprs)] struct ConsPeaks where [(); N]: Sized, { data: [u8; N], next: Option>>, } As far as I can guess a…
0
votes
1 answer

Expression: "with value" vs. "evaluate to value"

In the context of (constant) expressions: what is the difference between "with value" and "evaluate to value"? Here are some quotes from C11 (emphases added): The constant expression shall be an integer constant expression. It shall evaluate to…
pmor
  • 5,392
  • 4
  • 17
  • 36
0
votes
1 answer

Why does __builtin_constant_p evaluate to false for a constexpr std::string_view argument?

I need to interface with an API that expects compile time static strings. It enforces them via using static_assert (__builtin_constant_p (str)); To be more specific, I'm referring to the Apple Signpost API here. Now I have a situation where I'd…
PluginPenguin
  • 1,576
  • 11
  • 25
0
votes
1 answer

Common Lisp: Is the interpreter or compiler allowed to coalesce identical constant expressions?

I'm reading the description of DEFVAR in the CLHS (http://clhs.lisp.se/Body/m_defpar.htm). I am trying to determine what is conforming behavior for something like (defvar x '(1 2 3)) (defvar y '(1 2 3)) (nreverse x) Can that reverse Y as well? That…
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
0
votes
0 answers

Constant Expression Required on turbo C++ but on other Compiler, it works fine. C++ language

So I know that turbo C++ is an old compiler, but that is what our instructor told us to use. I keep getting Constant Expression Required, but on the other compiler it works just as fine. ` #include int main() { int numberV; …
0
votes
1 answer

How to make `this` pointer constant expression?

This is a follow-up question is my previous question: Why are member functions returning non-static data members not core constant expressions? The reduced version of the example mentioned in that question is: struct S { const bool x = true; …
mada
  • 1,646
  • 1
  • 15
0
votes
3 answers

Expression must have a constant value problem

I used C in Visual Studio to make a code for a user to input size of array. The code does not work in Visual Studio and gives errors. But on a site like replit it works. I don't understand what to do to make it work in Visual Studio. #include…
0
votes
0 answers

Java: Why are enum fields not considered constant at compile time?

I am attempting to use an enum to store ID values that I can use in a switch statement later. However despite the fact the id value is stored as final byte and that you cannot add new enum entries at runtime, the compiler seems unable to realise the…
tupto
  • 135
  • 2
  • 6
0
votes
2 answers

Evaluate constant expression during compile time

I am trying to write a preprocessor macro MYPRINTF(x, ...) that should work as printf but with an additional length specifier w. The definition of w is supposed to be operating system dependent, but on Linux it is equivalent to l. The code works…
aahlback
  • 82
  • 1
  • 5
0
votes
0 answers

Problem with Sympy lambdify with functions of 'x' or constants

I have working code using sympy.lambdify for a user input function string and then to plot the function. It fails if the function entered is just a constant number. The code I am using is import sympy as sy import numpy as np import…
KitingPaul
  • 49
  • 5
0
votes
2 answers

Misunderstanding Go Language specification on floating-point rounding

The Go language specification on the section about Constant expressions states: A compiler may use rounding while computing untyped floating-point or complex constant expressions; see the implementation restriction in the section on constants. This…
user18677817
0
votes
0 answers

Cannot use template function type in constant context with MSVC 1930+ (Visual Studio 2022)

When I have a function template like this: template T func(T bar) { return bar; } I cannot use its instantiation in constant context with the latest MSVC compiler: constexpr bool b = std::is_function_v)>; // fails…