4 ms·
I don't really use optional in C++, but it strikes me that it would be pretty hard to reason about the lifetime of the T&. Pointers have the same problem, but w
by overgard 5y ago
I don't really use optional in C++, but it strikes me that it would be pretty hard to reason about the lifetime of the T&. Pointers have the same problem, but when you see a pointer you're automatically going to start thinking about ownership, whereas references (especially if auto comes into play) are a lot more subtle...
Also if you're going to optionally store a reference why not just use a unique_ptr or a shared_ptr so ownership is much more obvious and safer. (I realize it might not be zero cost, but optional<T&> just seems like an invitation to hard to find bugs)