close
Пространства имён
Варианты
Действия

Заголовочный файл стандартной библиотеки <expected> (C++23)

Материал из cppreference.com
 
 
Заголовочные файлы стандартной библиотеки
Языковая поддержка
Концепты
<concepts> (C++20)
Диагностика
<system_error> (C++11)

Управление памятью
<memory_resource> (C++17)  
Метапрограммирование
<type_traits> (C++11)
<ratio> (C++11)
Общие утилиты
<utility>
<tuple> (C++11)
<optional> (C++17)
<variant> (C++17)
<any> (C++17)
<expected> (C++23)
<bitset>

<charconv> (C++17)
<format> (C++20)
<bit> (C++20)

Строки
<cuchar> (C++11)

Контейнеры
<flat_set> (C++23)
<span> (C++20)
<mdspan> (C++23)

Итераторы
<iterator>
Диапазоны
<ranges> (C++20)
<generator> (C++23)
Алгоритмы
Числа
<cfenv> (C++11)
<complex>
<numbers> (C++20)

Время
<chrono> (C++11)
Локализация
<codecvt> (C++11/17*)
Ввод/вывод
<filesystem> (C++17)
<cstdio>
<cinttypes> (C++11)
<strstream> (C++98*)
Регулярные выражения
<regex> (C++11)
Поддержка конкуренции
<stop_token> (C++20)
<thread> (C++11)
<atomic> (C++11)
<stdatomic.h> (C++23)
<mutex> (C++11)
<shared_mutex> (C++14)
<condition_variable> (C++11)  
<semaphore> (C++20)
<latch> (C++20)
<barrier> (C++20)
<future> (C++11)

Совместимость с C
<cstdbool> (C++11/17/20*)  
<ccomplex> (C++11/17/20*)
<ctgmath> (C++11/17/20*)

<cstdalign> (C++11/17/20*)

<ciso646> (до C++20)

 

Этот заголовок является частью библиотеки общих утилит.

Классы

(C++23)
оболочка, содержащая либо ожидаемое значение, либо значение ошибки
(шаблон класса) [править]
представлен как неожидаемое значение
(шаблон класса) [править]
исключение, указывающее проверенный доступ к expected, содержащему неожидаемое значение
(шаблон класса) [править]
тег создания на месте для неожидаемого значения в expected
(класс) (константа) [править]

Краткое описание

namespace std {
  // шаблонный класс unexpected
  template<class E> class unexpected;

  // шаблонный класс bad_expected_access
  template<class E> class bad_expected_access;

  // специализация bad_expected_access для void
  template<> class bad_expected_access<void>;

  // конструктор по месту значений unexpected
  struct unexpect_t {
    explicit unexpect_t() = default;
  };
  inline constexpr unexpect_t unexpect{};

  // шаблонный класс expected
  template<class T, class E> class expected;

  // частичная специализация expected для типов void
  template<class T, class E> requires is_void_v<T> class expected<T, E>;
}

Шаблонный класс cpp/header/expected/unexpected

namespace std {
  template<class E>
  class unexpected {
  public:
    // конструкторы
    constexpr unexpected(const unexpected&) = default;
    constexpr unexpected(unexpected&&) = default;
    template<class... Args>
      constexpr explicit unexpected(in_place_t, Args&&...);
    template<class U, class... Args>
      constexpr explicit unexpected(in_place_t, initializer_list<U>, Args&&...);
    template<class Err = E>
      constexpr explicit unexpected(Err&&);
    
    // присваивание
    constexpr unexpected& operator=(const unexpected&) = default;
    constexpr unexpected& operator=(unexpected&&) = default;
  
    // наблюдатели
    constexpr const E& error() const& noexcept;
    constexpr E& error() & noexcept;
    constexpr const E&& error() const&& noexcept;
    constexpr E&& error() && noexcept;

    // обмен
    constexpr void swap(unexpected& other) noexcept(/* смотрите описание */);

    friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y)));

    // оператор равенства
    template<class E2>
      friend constexpr bool operator==(const unexpected&, const unexpected<E2>&);

  private:
    E unex;    // только для пояснения
  };

  template<class E> 
    unexpected(E) -> unexpected<E>;
}

Шаблонный класс std::bad_expected_access

namespace std {
  template<class E>
  class bad_expected_access : public bad_expected_access<void> {
  public:

    // явный конструктор
    explicit bad_expected_access(E);

    // наблюдатели
    const char* what() const noexcept override;
    E& error() & noexcept;
    const E& error() const& noexcept;
    E&& error() && noexcept;
    const E&&  error() const&& noexcept;

  private:
    E unex;              // только для пояснения
  };
}

Специализация шаблонного класса std::bad_expected_access<void>

namespace std {
  template<>
  class bad_expected_access<void> : public exception {
  protected:
    // конструкторы
    bad_expected_access() noexcept;
    bad_expected_access(const bad_expected_access&);
    bad_expected_access(bad_expected_access&&);
    bad_expected_access& operator=(const bad_expected_access&);
    bad_expected_access& operator=(bad_expected_access&&);

    ~bad_expected_access();

  public:
    const char* what() const noexcept override;
  };
}

Шаблонный класс std::expected

namespace std {
  template<class T, class E>
  class expected {
  public:
    using value_type = T;
    using error_type = E;
    using unexpected_type = unexpected<E>;

    template<class U>
    using rebind = expected<U, error_type>;

    // конструкторы
    constexpr expected();
    constexpr explicit(/* смотрите описание */) 
      expected(const expected&);
    constexpr explicit(/* смотрите описание */) 
      expected(expected&&) noexcept(/* смотрите описание */);
    template<class U, class G>
      constexpr explicit(/* смотрите описание */) expected(const expected<U, G>&);
    template<class U, class G>
      constexpr explicit(/* смотрите описание */) expected(expected<U, G>&&);

    template<class U = T>
      constexpr explicit(/* смотрите описание */) expected(U&& v);

    template<class G>
      constexpr expected(const unexpected<G>&);
    template<class G>
      constexpr expected(unexpected<G>&&);

    template<class... Args>
      constexpr explicit expected(in_place_t, Args&&...);
    template<class U, class... Args>
      constexpr explicit expected(in_place_t, initializer_list<U>, Args&&...);
    template<class... Args>
      constexpr explicit expected(unexpect_t, Args&&...);
    template<class U, class... Args>
      constexpr explicit expected(unexpect_t, initializer_list<U>, Args&&...);

    // деструктор
    constexpr ~expected();

    // присваивание
    constexpr expected& operator=(const expected&);
    constexpr expected& operator=(expected&&) noexcept(/* смотрите описание */);
    template<class U = T> constexpr expected& operator=(U&&);
    template<class G>
      constexpr expected& operator=(const unexpected<G>&);
    template<class G>
      constexpr expected& operator=(unexpected<G>&&);

    template<class... Args>
      constexpr T& emplace(Args&&...) noexcept;
    template<class U, class... Args>
      constexpr T& emplace(initializer_list<U>, Args&&...) noexcept;

    // обмен
    constexpr void swap(expected&) noexcept(/* смотрите описание */);
    friend constexpr void swap(expected&, expected&) noexcept(/* смотрите описание */);

    // наблюдатели
    constexpr const T* operator->() const noexcept;
    constexpr T* operator->() noexcept;
    constexpr const T& operator*() const& noexcept;
    constexpr T& operator*() & noexcept;
    constexpr const T&& operator*() const&& noexcept;
    constexpr T&& operator*() && noexcept;
    constexpr explicit operator bool() const noexcept;
    constexpr bool has_value() const noexcept;
    constexpr const T& value() const&;
    constexpr T& value() &;
    constexpr const T&& value() const&&;
    constexpr T&& value() &&;
    constexpr const E& error() const&;
    constexpr E& error() &;
    constexpr const E&& error() const&&;
    constexpr E&& error() &&;
    template<class U> constexpr T value_or(U&&) const&;
    template<class U> constexpr T value_or(U&&) &&;

    // операторы равенства
    template<class T2, class E2>
      friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y);
    template<class T2>
      friend constexpr bool operator==(const expected&, const T2&);
    template<class E2>
      friend constexpr bool operator==(const expected&, const unexpected<E2>&);

  private:
    bool has_val;       // только для пояснения
    union {
      T val;            // только для пояснения
      E unex;           // только для пояснения
    };
  };
}

Частичная специализация std::expected для типов void

namespace std {
  template<class T, class E> requires is_void_v<T>
  class expected<T, E> {
  public:
    using value_type = T;
    using error_type = E;
    using unexpected_type = unexpected<E>;

    template<class U>
    using rebind = expected<U, error_type>;

    // конструкторы
    constexpr expected() noexcept;
    constexpr explicit(/* смотрите описание */)
      expected(const expected&);
    constexpr explicit(/* смотрите описание */)
      expected(expected&&) noexcept(/* смотрите описание */);
    template<class U, class G>
      constexpr explicit(/* смотрите описание */) expected(const expected<U, G>&);
    template<class U, class G>
      constexpr explicit(/* смотрите описание */) expected(expected<U, G>&&);

    template<class G>
      constexpr expected(const unexpected<G>&);
    template<class G>
      constexpr expected(unexpected<G>&&);

    constexpr explicit expected(in_place_t) noexcept;
    template<class... Args>
      constexpr explicit expected(unexpect_t, Args&&...);
    template<class U, class... Args>
      constexpr explicit expected(unexpect_t, initializer_list<U>, Args&&...);

    // деструктор
    constexpr ~expected();

    // присваивание
    constexpr expected& operator=(const expected&);
    constexpr expected& operator=(expected&&) noexcept(/* смотрите описание */);
    template<class G>
      constexpr expected& operator=(const unexpected<G>&);
    template<class G>
      constexpr expected& operator=(unexpected<G>&&);
    constexpr void emplace() noexcept;

    // обмен
    constexpr void swap(expected&) noexcept(/* смотрите описание */);
    friend constexpr void swap(expected&, expected&) noexcept(/* смотрите описание */);

    // наблюдатели
    constexpr explicit operator bool() const noexcept;
    constexpr bool has_value() const noexcept;
    constexpr void operator*() const noexcept;
    constexpr void value() const&;
    constexpr void value() &&;
    constexpr const E& error() const&;
    constexpr E& error() &;
    constexpr const E&& error() const&&;
    constexpr E&& error() &&;

    // операторы равенства
    template<class T2, class E2> requires is_void_v<T2>
      friend constexpr bool operator==(const expected& x, const expected<T2, E2>& y);
    template<class E2>
      friend constexpr bool operator==(const expected&, const unexpected<E2>&);

  private:
    bool has_val;        // только для пояснения
    union {
      E unex;            // только для пояснения
    };
  };
}