-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.

2
  • 2
    maybe clarify, what code would you like to write but cannot? Commented Jan 8, 2021 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. Commented Jan 8, 2021 at 15:59

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.