On Tue, Nov 18, 2014 at 4:23 AM, Andrzej Krzemienski
2014-11-18 8:00 GMT+01:00 Andrey Semashev
: On Mon, Nov 17, 2014 at 12:24 PM, Andrzej Krzemienski
wrote: The solution is based on the observation that there is a limited number
of
things you can do with optional<T>
optional<int> oi = ...;
1: if (oi) doSomething(*oi); else doSomethingElse(); // or nothing
2: if (oi) doSomething(*oi); else doSomething(-1); // use default value
3: if (!oi) oi = (int)getIntByOtherMeans(); doSomething(*oi); // now it is safe
Now we simply provide a dedicated interface for each of these three usages:
1: oi.use(&doSomething, &doSomethingElse); // or use lambdas
I'd call it visit(). Also, a one argument visit() could be useful (the visitor would be passed boost::none if the value is absent).
2: doSomething(oi.value_or(-1));
We already have that.
3: doSomething(oi.value_or_eval(getIntByOtherMeans));
So, basically, the proposal is to add visitation API, am I correct?
And to remove operator* and get() and value() and get_pointer() - anything that could cause UB. And remove implicit conversion from T
value() throws, not UB. (At least in std::experimental) I could see a "safer" or at least "more explicit" optional that didn't use operator*, but still allowed access via named functions. And maybe did less implicit conversion / comparison. Not exactly safer, but more explicit, which helps safety. Tony