-1

I can initialize a variable at compile time using constexpr, but this makes the variable immutable (i.e. const).

I want a variable that is guaranteed to be initialized at compile time, but that I can still mutate/modify at run-time.

cigien
  • 57,834
  • 11
  • 73
  • 112
SungJinKang
  • 409
  • 3
  • 9
  • 2
    maybe clarify, what code would you like to write but cannot? – 463035818_is_not_an_ai Jan 08 '21 at 15:52
  • 1
    Worth noting, although `constinit` exists in C++20, it can only be applied to static and thread-local objects. Its main purpose is to prevent the static initialization order fiasco, not to provide a "compile-time constant but mutable" variable. The latter concept is not that useful, but if you really need it, it can be emulated by making a non-`const` copy of a `constexpr` variable. – Brian Bi Jan 08 '21 at 15:59

1 Answers1

1

i think there should be non const and compile time evaluated variable.

Take a look at C++20's constinit.

Acorn
  • 24,970
  • 5
  • 40
  • 69