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
5
votes
0 answers

How to understand the sentence "full-expression must be a constant expression" which mentioned in the standard

Ago, Some rules in the standard that say they are applied to expression, I was confused about whether these rules can also be applied to full-expression arbitrarily. I get an answers in that question. However, there are some rules like "the…
xmh0511
  • 7,010
  • 1
  • 9
  • 36
5
votes
3 answers

How to set string (or AnsiString) constant in the TVarRec?

I want to pass the formatting arguments Args into the Format function. I found some examples of that, but I can't find out how to assign string constant in the TVarRec member. The following code fails on compilation with E2089 Invalid…
user532231
5
votes
1 answer

Is const_cast valid in a constant expression? (C++14, C++17)

The specific issue that a came across is that there is some inconsistency in how compilers handle it. For instance this code (https://godbolt.org/z/08Z-zi): constexpr auto value = 1; static_assert(*const_cast(&value), "value should be…
5
votes
5 answers

Can I get a Rust array's length with only a type, not a concrete variable?

I want to rewrite the following C++ code into Rust: using storage = array; const size_t storage_len = sizeof(storage) / sizeof(storage::value_type); How can I get that constant length value without a concrete variable? As motivation,…
jeiea
  • 1,965
  • 14
  • 24
5
votes
2 answers

Why can't a static initialization expression in C use an element of a constant array?

The following (admittedly contrived) C program fails to compile: int main() { const int array[] = {1,2,3}; static int x = array[1]; } When compiling the above C source file with gcc (or Microsoft's CL.EXE), I get the following error: error:…
deltamind106
  • 638
  • 7
  • 19
5
votes
1 answer

static const double initialization in C++

I have some legacy code that I'm building with "newer" compilers and running into some static const double initialization errors that don't make sense to me. Here's what I have: //header.h class myclass { private: static const double foo =…
Mr_Gr1mm
  • 61
  • 4
5
votes
2 answers

Why can't I turn this string into a Literal?

I need to convert a string into a Literal so I can pass it as an argument to CsvProvider. But I am not able to do it. The code below runs with no problems: open System.IO open FSharp.Data open FSharp.Data.JsonExtensions let charSwitch (a: char) b x…
Soldalma
  • 4,636
  • 3
  • 25
  • 38
5
votes
2 answers

Inconsistent evaluation for `constexpr` lambdas in templates between `static_assert`, `if constexpr(...)` and `constexpr` variables

(Using g++ 7.0 trunk.) Given the following "type-to-value wrapping" utilities... template struct type_wrapper { using type = T; }; // "Wraps" a type into a `constexpr` value. template constexpr type_wrapper
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
5
votes
2 answers

In-class static member initialization

Given struct X {}; constexpr auto x = X{}; struct S { static constexpr auto& rx = x; }; gcc 4.8 says error: non-constant in-class initialization invalid for static member 'S::rx' static constexpr auto& rx = x; …
4
votes
2 answers

constexpr general confusion

I have made a thread yesterday but I think that it was unclear and the replies I got didn't solve my confusion at all. So, I'll try to make the example simpler. Why is this allowed: constexpr int incr(int k1) { return k1 + 5; } constexpr int…
4
votes
2 answers

Should user defined literals always be consteval in C++20?

If I'm not mistaken, the arguments for a user defined literal are always known at compile time. In C++20 you can force functions to execute at compile time using consteval so that throw generates a compile time error. #include consteval…
4
votes
1 answer

Why does constinit allow UB?

Say I initialize variables like this: #include constexpr uint16_t a = 65535; constinit int64_t b = a * a; // warning: integer overflow in expression of type 'int' results in '-131071' [-Woverflow] constexpr int64_t c = a * a; // error:…
4
votes
0 answers

Inner enum classes of templates not considered constant expressions?

I'm using C++ with GCC's C extension for designated inits / designated array initializers and I've come across a bit of a pickle. If I have the following (contrived) example code, #include #include struct STRUCTURE { enum…
13steinj
  • 418
  • 5
  • 12
4
votes
2 answers

Is there *any* situation under which "for _ in [1,2,3]" will not loop at all?

I was writing some code and made a mistake that simplifies to: func f() -> Int { for _ in [1,2,3] { return 1 } } And the compiler shows me an error saying that f is missing an return, which caused me to realise my mistake. I forgot…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
4
votes
2 answers

cannot appear in a constant-expression

In the following c++ program: static const int row = (dynamic_cast(log(BHR_LEN*G_PHT_COUNT)/log(2))); static const int pht_bits = ((32*1024)/(G_PHT_COUNT * G_PHT_COUNT * BHR_LEN)); unsigned char tab[pht_bits][1<
Kunal Vyas
  • 1,499
  • 1
  • 23
  • 40