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 )) {

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

Feedback about page:

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

Table Of Contents