
Vaclav Vesely wrote:
Hi,
There is common need of getting value from optional with possibility to define default value in case of the optional is uninitialized. I suggest two alternatives:
optional<int> o; int i = o.get(123); // meber function get with additional parameter int j = from_optional(o, 123); // free function
Both alternatives can be implemented simultaneously.
What do you think about it?
Hi Vaclav, Indeed, this has been requested in the past and the fact that you can't use an rvalue twice via the ternary operator makes it worth it. It won't make it into 1.34 though, so I'll add it after the release. As for the name, I like "from_optional" better than anything else, it goes along with another pending addition: make_optional(cond,value) which is useful in cases when you do compute some value, but, due to some condition (like a validity check on the value) you rather not return it; the alternative: cond ? optional<T>(value) : optional<T>() ; is much much cumbersome if T is a complex type expression (and the purpose of make_optional() is to deduce T for you). Best -- Fernando Cacciola SciSoft http://fcacciola.50webs.com/