I would like to implement some constexpr logic in some piece of code. I am able to compile and execute this code without problem.
#include <iostream>
int main(){
std::cout << "Starting c++ main" << std::endl;
constexpr int val_1 = 100;
int val_2;
if constexpr (val_1 == 100){
val_2 = 10;
}
else if constexpr (val_1 == 200){
val_2 = 20;
}
std::cout << val_2 << std::endl;
}
But really, the val_2 should be a constexpr too. How can I get val_2 to be a constexpr? I tried several things, but with no success. If I declare val_2 as constexpr, then I am not allowed to change its value in the if statement. If I do not declare the constexpr variable before the body of the if statements, then it is (as expected) not defined outside of the if statement.