Just use
std::variant<int*, ...>
References in C++ are just sugary pointers. std::variant<int*, ...>
References in C++ are just sugary pointers.void test(int& y){}
int main() { int* x = nullptr; test(*x); }
You could design a language feature where integer to enum is checked, but that's not enum.
Enum classes already add scoping, forbid implicit conversions and allow explicit underlying types. Those are pure extensions. Making undeclared values invalid or UB would be very surprising to people used to normal enums.
What I have seen is automatic variables escaping their scope as a reference, which rust protects against. And is also much more dangerous, because dereferencing a nullptr is defined behavior on most platforms