There is an ongoing debate about what optional
and variant
should do with reference types, particularly with regards to assignment. I would like to better understand the debate around this issue.
optional<T&> opt;
opt = i;
opt = j; // should this rebind or do i=j?
Currently, the decision is to make optional<T&>
ill-formed and make variant::operator=
ill-formed if any of the types is a reference type - to sidestep the argument and still give us most of the functionality.
What is the argument that opt = j
should rebind the underlying reference? In other words, why should we implement optional
like this:
template <class T>
struct optional<T&> {
T* ptr = nullptr;
optional& operator=(T& rhs) {
ptr = &rhs;
return *this;
}
};