-1

C++ why compile time variable is immutable??

i don't know why compile time variable (constexpr) is immutable?? i think const variable is valuable. because it stop developer change value by mistake

but why does constexpr force const and compile time evaluation both. i think there should be non const and compile time evaluated variable. but there isn't

and i think compile time variable can be changed. accessing to variable memory and modifying look not impossible.

but why i can't change compile time evaluated variable??

compile time variable isn't store at memory??

SungJinKang
  • 327
  • 1
  • 7
  • 2
    maybe clarify, what code would you like to write but cannot? – 463035818_is_not_a_number 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

2

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

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

Acorn
  • 23,483
  • 4
  • 35
  • 66