I always assumed:
- writing to
const_casted variable is UB - there is no UB allowed in constexpr
So I am confused why this code compiles:
constexpr int fn(){
int v = 42;
return [v]() {
const_cast<int&>(v)+=5;
return v;
}();
}
static constexpr auto val = fn();
int main() {
return val;
}
note: I know there is no reason to not allow this to work since it is obvious what the result should be, I am more interested in the legal explanation why is this allowed.
return [x = v]() { ...conston the firstvis important. I think I should catch some sleep...