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
2
votes
1 answer

`const int a = 1;` is `a` a constant expression, if `a` has automatic storage duration

N4527 5.20[expr.const]p2 A conditional-expression e is a core constant expression unless the evaluation of e, following the rules of the abstract machine (1.9), would evaluate one of the following expressions: (2.7) — an lvalue-to-rvalue…
stackcpp
  • 1,275
  • 7
  • 15
2
votes
2 answers

Static Final Ints in Switch: Why can't this be done?

I had a Switch referencing Resource Ids from R.java in a Library Project: switch (code) { case R.id.code_one: blah(); break; case R.id.code_two: bleh(); break; } From ADT 14, R fields are no longer final, so…
2
votes
3 answers

Setting an array's size to the value of a const derived from the division of another constant

I have these two supposed constants: int const MATRIX_SIZE = 1000; int const LONGEST_LR_LINK = (int)floor(MATRIX_SIZE/2); I am attempting to declare an array: int lrLinkArray [LONGEST_LR_LINK]; I get the error: error: array bound is not an…
Gerald
  • 521
  • 1
  • 6
  • 16
2
votes
1 answer

Redundant instantiations in boolean constant expressions

I have an n-any boolean OR run-time function any_run #include bool any_run() { return false; } template bool any_run(bool a, B... b) { assert(a); return a || any_run(b...); } as well as a compile-time analogue…
2
votes
3 answers

What is the relationship between constant-expression and conditional-expression?

I want to have a precise comprehension about the constant expression of C. So, I read the related provisions of C99. The first provision in chapter "6.6 Constant expressions" describes the syntax of constant expression, but I can't understand…
junwanghe
  • 187
  • 1
  • 7
1
vote
1 answer

B tree class implementation: expression must have constant values?

Suppose that we have a "itemtype.h" header file, where I declare the following items: #include #include using namespace std; const int keyfieldmax=12; const int kfmaxplus=keyfieldmax+1; const int datafieldmax=36; const int…
user466534
1
vote
2 answers

What happens if an assumption, i.e. [[assume]] fails in a constant expression?

In C++23, the [[assume(conditonal-expression)]] attribute makes it so that if conditional-expression doesn't evaluate to true, the behavior is undefined. For example: int div(int x, int y) { [[assume(y == 1)]]; return x / y; } This compiles…
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
1
vote
1 answer

Constexpr / Compile time constant expressions bugs

I get the following error in my project upon compiling. Tried everything but unable to resolve this. Reproduced the error here: https://replit.com/join/egfoiwpgig-darshanpandhi Error error: constexpr variable 'noOfTiles' must be initialized by a…
1
vote
0 answers

Mismatch between C++17 and C++20, const char* no longer a constant expression

I have a project where I use the spdlog library. My header file looks like this: #pragma once #include #include #include namespace vtek { void initialize_logging(const InitInfo* info); void…
alexpanter
  • 1,222
  • 10
  • 25
1
vote
2 answers

Temporary objects that are usable in constant expressions

As a follow-up to this question, clang accepts the code provided there. This question has the following code: constexpr int func(int const& rf){ return rf; } int main(){ constexpr int value = func(0); } This question has a good answer, but…
mada
  • 1,646
  • 1
  • 15
1
vote
0 answers

Why this expression is not constant expression

I have the following code: struct S { const bool x = true; constexpr const bool& f() { return x; } } s{}; int main() { static_assert(s.f()); // error: s is not declared as 'constexpr'; } Note that, this is a dupe for the question How to…
mada
  • 1,646
  • 1
  • 15
1
vote
1 answer

How to implement std::lcm to be standard conform?

I'm trying to implement std::lcm and I need to static_assert that the arguments or result are within bounds as required by the standard: The behavior is undefined if |m|, |n|, or the least common multiple of |m| and |n| is not representable as a…
Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42
1
vote
2 answers

Why exactly using of a floating-point arithmetic in an integer constant expression is invalid?

In C11 (and later) integer constant expression shall only have operands that are, in particular: floating constants that are the immediate operands of casts The following code: int a[ A > B ? 16 : 32 ]; when A and B are floating constants is…
pmor
  • 5,392
  • 4
  • 17
  • 36
1
vote
2 answers

For constant expressions, why can't I use a use a pointer-type object, as standards says?

I was trying to figure out the restrictions of constexpr in cpp11/14. There are some usage requirements I found in CPP14-5.19-4: A constant expression is either a glvalue core constant expression whose value refers to an object with static storage…
absuu
  • 340
  • 1
  • 11
1
vote
1 answer

Why global variables are not known at compile-time?

In C++, I know that if a I declared a variable inside a function, this variable is actually considered as auto local variable (destroyed once a function does return). So it stands to reason that, a local variable cannot appears in a constant…
mada
  • 1,646
  • 1
  • 15