Wrong analogy. References in C++ are not first-class entities. Conceptually, they're _alias names_ for existing objects. Unlike C# and Java "references", you cannot "reset" a C++ reference to "point" to another object. Because it's not a pointer. It does not "point". It's an alias. `void f(int& ref)` in C++ is the same as `void f(ref int x)` in C# (the equivalent doesn't exist in java). Inside `f`, you cannot change `x` to "point to some other int" because it's not a pointer!
C# and Java "references" are semantically the same as C++ pointers minus arithmetic.
This could've been done since 2003 but wasn't. There are attempts to bring it into language as part of C++ Core Guidelines project.
This is possible because unlike Java and C# not everything can be null.
But you can entirely write a shitton of C++ programs that won't use pointers at all - not even shared / unique. In contrast, you can't avoid C# and Java references.
template<typename T> class List<T> {
protected:
List();
template<typename U> class Visitor<U> {
public:
U visitNil();
U visitCons(T current, U accumulator);
}
public:
virtual <U> U visit(Vistor<U> visitor);
}
template<typename T> class Nil<T> extends List<T> {
U visit(Visitor<U> visitor) = visitor.visitNil();
}
template<typename T> class Cons<T> extends List<T> {
private:
T head;
List<T> &tail;
public:
Cons(T head, List<T> &tail) {
this.head = head;
this.tail = tail;
}
U visit(Visitor<U> visitor) = visitor.visitCons(head, tail.visit(visitor));
}
References in C++ are non-nullable so the precedent already existed.
I hope C# gets around to solving this problem -- Microsoft is working on it -- but it's much harder to retroactively solve it.