4 ms·
Well, front() returns a reference. std::optional can't hold references. The closest you get is a plain pointer, with a null pointer for an empty container.
by planede 4y ago
Well, front() returns a reference. std::optional can't hold references. The closest you get is a plain pointer, with a null pointer for an empty container.
- dureuill 4y agoOther languages allow to return `Option<&T>`. That's what I'm using these days. I suppose a raw ptr is what I would use on C++ to model that case though, yes.
- planede 4y agoYeah, for some reason `std::optional<T&>` is cursed and can't get through standardization. People can't decide what should happen on assignment.
- dureuill 4y agoI guess a raw pointer is good enough an approximation in that case, it is just annoying that suddenly you can no longer use an optional because your type is a reference