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
2 answers

Is there a standard way to guarantee that a certain (constant) expressions will be evaluated at compile (translation) time?

I am quite surprised that C does not guarantee that certain (constant) expressions are evaluated at compile (translation) time. C11 (6.6 Constant expressions) (emphasis added): A constant expression can be evaluated during translation rather than…
pmor
  • 5,392
  • 4
  • 17
  • 36
1
vote
1 answer

Applying the lvalue-to-rvalue conversion to variable `x` does not yield a constant expression in the code below. Why is that?

The snippet below was obtained from DR 2083 by Hubert Tong. extern int globx; int main() { const int &x = globx; struct A { const int *foo() { return &x; } } a; return *a.foo(); } Then the author writes: x satisfies the…
João Afonso
  • 1,934
  • 13
  • 19
1
vote
3 answers

Can array length in declaration be non-constant?

I am a bit confused about array declaration in C. I know that it's possible to do this: int a[20]; // Reserved space for 20 int array int b[] = {32, 431, 10, 42}; // Length in square brackets is auto-calculated int *c = calloc(15, sizeof(int)); …
Vlad Havriuk
  • 1,291
  • 17
  • 29
1
vote
2 answers

Better Way to Use Switch Case?

so my problem is that I'm trying to write a program that uses a switch case to tell a function which case to do. I tried to include an or statement into the switch cases so that I could have a range of values for each case but it seems to be…
1
vote
2 answers

Is Build.VERSION.SDK_INT constant expression?

Given the boolean and a variable set based on it: private static final boolean IS_ANDROID_Q = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; private static final int OPTION = IS_ANDROID_Q ? 0 : 1; There is a switch-case statement: public static…
PHD
  • 595
  • 1
  • 8
  • 18
1
vote
1 answer

C - problem with const - error: initializer element is not constant

I try add global variable in my buttons.c file, but have a error - initializer element is not constant. Example headers.h file struct MainStruct { GtkEntryBuffer *buffer; GtkWidget *entry; GtkWidget *label; }; extern struct MainStruct…
user9690594
1
vote
4 answers

C# string must be a constant

I was writing this piece of code: public const int MAJOR_VERSION = 3; public const int MINOR_VERSION = 3; public const string VERSION_STRING = $"v.{MAJOR_VERSION}{MINOR_VERSION}"; And, maybe not surprisingly, the compiler was complaining: "The…
alexpanter
  • 1,222
  • 10
  • 25
1
vote
1 answer

How does one pass an integer, to a pointer, to an std::array, while satisfying a constnt expression?

I have a function noise.cpp which is currently of the form, double* noise(int* steps, ...) //some code std::array NoiseOut; //rest of code Which is being tested and accessed by cppnoise.cpp, #include #include…
1
vote
1 answer

Error (10454): VHDL syntax error at fft_engine.vhd(151): right bound of range must be a constant

I am working on Quartus Prime, and I am having issue (on line 13) with the error: Error (10779): VHDL error at fft_engine.vhd(154): expression is not constant This shift to the variable k_uns is not synthesis code (as I have understood), however I…
maigipra
  • 11
  • 2
1
vote
1 answer

const pointers passed as argument into constexpr function

I am doing some very basic problems to get familiar with Template Metaprogramming. This one in particular is just to add up the values on the down-right diagonal of a square matrix (indexes of [0,0],[1,1], etc.). I have solved this in many ways with…
Saswat Mishra
  • 185
  • 1
  • 11
1
vote
1 answer

qMetaTypeID doesn't evaluate to constant expression?

I'm trying to get the ID of a QMetaType at compile time, however when I try this very simple base case: CMakeLists.txt cmake_minimum_required(VERSION 3.10) project(helloqt) set(CMAKE_CXX_STANDARD 14) find_package(Qt5Widgets…
Krupip
  • 4,404
  • 2
  • 32
  • 54
1
vote
1 answer

Core constant expression and array indexing

The reference is to the code segment under the following: Core constant expressions int main() { const std::size_t tabsize = 50; int tab[tabsize]; // OK: tabsize is a constant expression std::size_t n = 50; const std::size_t sz =…
user9196120
  • 381
  • 3
  • 9
1
vote
0 answers

java undefined compile time constant

JavaSE 6 defines the rules of Definite assignment However, it's possible to assign a compile-time constant's value to its own undefined value, which results in weird behavior: Main.java public class Main{ public static final boolean CONST_TEST…
Andrei Mărcuţ
  • 1,273
  • 17
  • 28
1
vote
1 answer

Implicit type of constant in swift tutorial

When I do example from tutorial, I get some issue from constants variables topic. If someone explain my example I'll be appreciate for this.
Koray Yavic
  • 93
  • 1
  • 4
1
vote
2 answers

constant expression function in c++98

I have following problem with c++98 constant expressions. Here is an example for an template struct.. which will receive the size at compile time.. Is it somehow possible to get this size as constant expression without c++11 constexpr? Take a look…