folly::getRefOrDefault

getRefOrDefault overloads

Synopses

Declared in <folly/GetRefUtil.h>

Returns optRef.value() if present, otherwise a const reference to a default-constructed static value_type. Avoids the copy that value_or({}) would make.

template<IsOptionalLike T>
T::value_type const&
getRefOrDefault(T const& optRef);
» more...

Returns *ptr if non-null, otherwise a const reference to a default-constructed static T. Avoids copies compared to a ternary that constructs a temporary default.

template<typename T>
T const&
getRefOrDefault(T const* ptr);
» more...

Return Value

  • A const reference to the contained value, or to a default-constructed value.
  • A const reference to *ptr, or to a default-constructed value.

Parameters

NameDescription
optRefThe optional-like object to read from.
ptrThe pointer to dereference, which may be null.