folly::tryTo

tryTo overloads

Synopses

Declared in <folly/Conv.h>

String or StringPiece to target conversion. Accepts leading and trailing whitespace, but no non-space trailing characters.

template<class Tgt>
Expected<Tgt, /* implementation-defined */>
tryTo(StringPiece src) noexcept
requires !std::is_same<StringPiece, Tgt>::value;
» more...

struct timespec to std::chrono::duration

template<typename Tgt>
Expected<Tgt, ConversionCode>
tryTo(timespec const& ts)
requires detail::is_duration<Tgt>::value;
» more...

struct timeval to std::chrono::duration

template<typename Tgt>
Expected<Tgt, ConversionCode>
tryTo(timeval const& tv)
requires detail::is_duration<Tgt>::value;
» more...

tryTo/to that take the strings by pointer so the caller gets information about how much of the string was consumed by the conversion. These do not check for trailing whitespace.

template<class Tgt>
Expected<Tgt, /* implementation-defined */>
tryTo(StringPiece* src) noexcept;
» more...

Unchecked conversion from arithmetic to boolean. This is different from the other arithmetic conversions because we use the C convention of treating any non-zero value as true, instead of range checking.

template<
    class Tgt,
    class Src>
Expected<Tgt, ConversionCode>
tryTo(Src const& value) noexcept
requires is_arithmetic_v<Src> && !std::is_same<Tgt, Src>::value &&
        std::is_same<Tgt, bool>::value;
» more...

Enum to anything and back

template<
    class Tgt,
    class Src>
Expected<Tgt, ConversionCode>
tryTo(Src const& value) noexcept
requires std::is_enum<Src>::value && !std::is_same<Src, Tgt>::value &&
        !std::is_convertible<Tgt, StringPiece>::value;
» more...

Checked conversion from any type to an enum, returning an Expected.

template<
    class Tgt,
    class Src>
Expected<Tgt, ConversionCode>
tryTo(Src const& value) noexcept
requires !std::is_convertible<Src, StringPiece>::value && std::is_enum<Tgt>::value &&
        !std::is_same<Src, Tgt>::value;
» more...

Checked conversion between arithmetic types, returning an Expected.

template<
    typename Tgt,
    typename Src>
Expected<Tgt, ConversionCode>
tryTo(Src const& value) noexcept
requires detail::IsArithToArith<Tgt, Src>::value;
» more...

timespec or timeval to std::chrono::time_point

template<
    typename Tgt,
    typename Src>
Expected<Tgt, ConversionCode>
tryTo(Src const& value)
requires detail::is_time_point<Tgt>::value && detail::is_posix_time_type<Src>::value;
» more...

overloadbrief to, but return an Expected

template<
    class Tgt,
    class Src>
Expected<Tgt, ConversionCode>
tryTo(Src&& value) noexcept
requires std::is_same<Tgt, typename std::decay<Src>::type>::value;
» more...

std::chrono::duration to struct timespec

template<
    typename Tgt,
    typename Rep,
    typename Period>
Expected<Tgt, ConversionCode>
tryTo(std::chrono::duration<Rep, Period> const& duration)
requires std::is_same<Tgt, struct timespec>::value;
» more...

std::chrono::duration to struct timeval

template<
    typename Tgt,
    typename Rep,
    typename Period>
Expected<Tgt, ConversionCode>
tryTo(std::chrono::duration<Rep, Period> const& duration)
requires std::is_same<Tgt, struct timeval>::value;
» more...

std::chrono::time_point to timespec or timeval

template<
    typename Tgt,
    typename Clock,
    typename Duration>
Expected<Tgt, ConversionCode>
tryTo(std::chrono::time_point<Clock, Duration> const& timePoint)
requires detail::is_posix_time_type<Tgt>::value;
» more...

String represented as a pair of pointers to char to unsigned integrals. Assumes NO whitespace before or after.

template<typename Tgt>
Expected<Tgt, ConversionCode>
tryTo(
    char const* b,
    char const* e) noexcept
requires is_integral_v<Tgt> && !std::is_same<Tgt, bool>::value;
» more...

Return Value

  • An Expected holding the converted value, or an error.
  • The converted duration, or an error code.
  • An Expected holding the resulting boolean.
  • An Expected holding the converted value, or an error code.
  • An Expected holding the converted enum value, or an error code.
  • The converted time point, or an error code.
  • The converted timespec, or an error code.
  • The converted timeval, or an error code.
  • The converted POSIX time value, or an error code.
  • An Expected holding the parsed value, or an error code.

Parameters

NameDescription
srcThe input string to convert.
tsThe timespec to convert.
tvThe timeval to convert.
valueThe arithmetic value to convert.
durationThe duration to convert.
timePointThe time point to convert.
bPointer to the first character of the input.
ePointer past the last character of the input.