std::optional as return value

suggest change
std::optional<float> divide(float a, float b) {
  if (b!=0.f) return a/b;
  return {};
}

Here we return either the fraction a/b, but if it is not defined (would be infinity) we instead return the empty optional.

A more complex case:

template<class Range, class Pred>
auto find_if( Range&& r, Pred&& p ) {
  using std::begin; using std::end;
  auto b = begin(r), e = end(r);
  auto r = std::find_if(b, e , p );
  using iterator = decltype(r);
  if (r==e)
    return std::optional<iterator>();
  return std::optional<iterator>(r);
}
template<class Range, class T>
auto find( Range&& r, T const& t ) {
  return find_if( std::forward<Range>(r), [&t](auto&& x){return x==t;} );
}

find( some_range, 7 ) searches the container or range some_range for something equal to the number 7. find_if does it with a predicate.

It returns either an empty optional if it was not found, or an optional containing an iterator tothe element if it was.

This allows you to do:

if (find( vec, 7 )) {
  // code
}

or even

if (auto oit = find( vec, 7 )) {
  vec.erase(*oit);
}

without having to mess around with begin/end iterators and tests.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


std::optional:
* std::optional as return value

Table Of Contents
8 Arrays
11 Loops
31 std::optional
39 Streams
51 Unions
56 Lambdas
60 SFINAE
62 RAII
67 Sorting
84 RTTI
87 Scopes
104 Profiling
107 Recursion
117 Iteration
125 Alignment
134 Semaphore
136 Debugging
139 Mutexes
142 decltype