4 ms·
Beware that his example of boost::optional is, as far as I know, wrong. He returns an empty student, which you can't test for (unless your Student() has a way o
by roel_v 11y ago
Beware that his example of boost::optional is, as far as I know, wrong. He returns an empty student, which you can't test for (unless your Student() has a way of telling you that it's default-constructed, and when a default-constructed Student is in fact the same as a 'null' Student). Instead of return boost::optional<Student>(); he should have done return boost::none; .
- KayEss 11y agoIf that's really the API for boost optional then I'm very pleased I never used it. A default constructed optional really needs to be empty rather than contain a default constructed value.
- vp2015 11y agoAre you sure about that? Both resources below say something opposite. http://bit.ly/1SEnJQe http://bit.ly/1SEnJQe - see optional<T>::optional() and http://bit.ly/1lh7eyv http://bit.ly/1lh7eyv - see default constructor (I'm just curious) Edit: I've tested it, you are wrong. Default constructor of optional doesn't call Student constructor (VS2015).
- roel_v 11y agoYes you're right, I misread. I still think the preferred idiom is to use boost::none (like the optional documentation does).
- vp2015 11y agoIn fact boost::optional examples use default constructor in this case - at least all examples I've seen, see http://www.boost.org/doc/libs/1_58_0/libs/optional/doc/html/boost_optional/examples.html http://www.boost.org/doc/libs/1_58_0/libs/optional/doc/html/...