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

templated operator "+" overloading and enum initialization [constexpr][msvc]

Please, consider the following code: template T operator+(const T& lvh, const T& rvh) { return lvh; } struct enum_container { enum { item1 = 1, item2 = 2, item3 = item1 + item2 }; }; int main() { …
2
votes
1 answer

Conditional inclusion: integral constant expression is unlimited?

Per C++11 (and newer) this code is valid: #if 1.0 > 2.0 ? 1 : 0 #endif However, most (if not all) C++ compilers reject it: $ echo "#if 1.0 > 2.0 ? 1 : 0" | g++ -xc++ - -std=c++11 -pedantic -c :1:5: error: floating constant in preprocessor…
pmor
  • 5,392
  • 4
  • 17
  • 36
2
votes
2 answers

Benefit of using final variables in Java 17 switch-case

Can someone tell me the benefit of Java 17 accepting final expressions as a case expression in switch-case-constructs, but does not accept the final expression to be passed as a parameter? void test(int distinction, final int foo) { …
JotA
  • 23
  • 4
2
votes
2 answers

C constant expressions. const variables as constant expressions

As per 6.6.10 10 An implementation may accept other forms of constant expressions. the implementation may consider const (or even not const as answers are focused on it - but it is not the core of the question) variables as constant…
0___________
  • 60,014
  • 4
  • 34
  • 74
2
votes
2 answers

Initializer element is not a compile-time constant using C

a.h list* FunctionNamesCreate(); list* const FunctionNames = FunctionNamesCreate(); a.c list* FunctionCreate() { list* FunctionNames = listCreate(sizeof(char*)); listPushHead(FunctionNames,"s"); return FunctionNames; } list is simple…
user13088490
2
votes
1 answer

Dart: Constant evaluation error. The method '[]' can't be invoked in a constant expression

I am getting an error on constant evaluation. please take a look at this code: class A { final num a; const A(this.a); } class B { final A a; const B(this.a); } main() { const a = A(12); const b = B(a); // this works fine …
2
votes
0 answers

Why are constexpr functions possibly ill-formed, NDR (10.1.5)?

Paragraph 10.1.5 says that a program is ill-formed, no diagnostic required, if a function is declared constexpr but no set of arguments exist that make it evaluable at compile-time. What's the rationale behind this? Since it's not feasible for the…
2
votes
1 answer

when to use template non-type classes or plain arguments in constexpr functions

I am very unclear when to use non-type template arguments (C++20) or normal arguments in constexpr functions. It's unclear to me, what the restrictions are and when to switch from pure parameters to non-type template parameters (see Live). Here an…
Gabriel
  • 8,990
  • 6
  • 57
  • 101
2
votes
2 answers

Initialization of a constexpr variable

[dcl.constexpr] p10 sentence 3 says: In any constexpr variable declaration, the full-expression of the initialization shall be a constant expression However, in this declaration: constexpr int a = 10; constexpr int b = a; a is not a constant…
2
votes
3 answers

Is there a correct constant-expression, in terms of a float, for its msb?

The problem: given a floating point constant expression, can we write a macro that evaluates to a constant expression whose value is a power of two equal to the most significant place of the significand? Equivalently, this is just the greatest power…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
2
votes
2 answers

C++ How to convince constexpr function to be constexpr, for any kind of argument

I could not find a relevant question/answer for this one. Consider this: // constexpr declares intent template inline constexpr const bool probe (T const &) noexcept { return false; } template inline constexpr const bool…
user10133158
2
votes
3 answers

Why references can't be used with compile time functions?

I have two snippets. The first snippet: #include template constexpr bool foo(T&&) { return false; } int main() { std::string a; if constexpr (foo(a)) { } } The second snippet: #include template…
nicolai
  • 1,140
  • 9
  • 17
2
votes
1 answer

Comparing ALAssetGroupType in switch statement

Hi I am calling ALAssetsLibrary's -enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:block failureBlock:failure; then inside the enumeration block i want to compare the type of group returned and add it to the relevant array. I have tried ^(…
2
votes
1 answer

Delphi Constant Expression Expected

I get a "Constant expression expected" error with the following code: TBoard is defined as: TBoard = class field: array[1..5,1..5] of Integer; function check(const x, y: Integer): Integer; function addShip(x, y, size, dir: Integer):…
Ricardo Boss
  • 101
  • 3
  • 12
2
votes
3 answers

How to force GCC compiler to calculate constants at compile-time with -Os

I tried to calculate hashes for constant C-strings in compile-time using macros. That is my example code: #include #include typedef uint32_t hash_t; #define hash_cstr(s) ({ \ typeof(sizeof(s)) i = 0; \ …