一、expectedscalartypelongbut的定義
template<typename T>
class expected {
public:
// construction
expected() = delete;
template<typename... Args,
typename = std::enable_if_t<std::is_constructible_v<T, Args...>>>
explicit expected(in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args...>);
template<typename U, typename... Args,
typename = std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args...>>>
explicit expected(in_place_t, std::initializer_list<U> ilist, Args&&... args) noexcept(std::is_nothrow_constructible_v<T, std::initializer_list<U>&, Args...>);
template<typename U = T,
typename = std::enable_if_t<std::is_copy_constructible_v<U>>>
expected(const expected& rhs);
template<typename U = T,
typename = std::enable_if_t<std::is_move_constructible_v<U>>>
expected(expected&& rhs) noexcept(std::is_nothrow_move_constructible_v<T>);
template<typename U,
typename = std::enable_if_t<std::is_constructible_v<T, U>>>
expected(U&& v);
template<typename U,
typename = std::enable_if_t<std::is_constructible_v<T, const U&>>>
expected(const U& v);
template<typename U, typename... Args,
typename = std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args...>>>
expected(std::initializer_list<U> il, Args&&... args);
template<typename U,
typename = std::enable_if_t<std::is_constructible_v<T, U>>>
expected& operator=(U&& v);
template<typename U,
typename = std::enable_if_t<std::is_constructible_v<T, const U&>>>
expected& operator=(const U& v);
// access
constexpr const T* operator->() const;
constexpr T* operator->();
constexpr const T& operator*() const&;
constexpr T& operator*() &;
constexpr explicit operator bool() const noexcept;
constexpr bool has_value() const noexcept;
// error handling
constexpr const E& error() const&;
constexpr E& error() &;
// swap
void swap(expected& rhs) noexcept(std::is_nothrow_move_constructible_v<T>&& std::is_nothrow_swappable_v<T>&& std::is_nothrow_swappable_v<E>);
// unexpected value handling
template<typename F>
expected<T, E>& map_error(F f);
};
expectedscalartypelongbut 是C++ STL庫中的一種類型,它被用於函數返回值,它的主要實現思路是:當函數的返回值存在錯誤時,返回一個 expected 對象,以此來告訴調用方已經發生了錯誤,可以通過該對象的 error() 方法獲取到錯誤信息。
二、expectedscalartypelongbut的構造函數
expectedscalartypelongbut 提供了多個構造函數,下面簡要介紹這幾種常用的構造函數:
1. expected()
這個構造函數是 expectedscalartypelongbut 類的默認構造函數,但是這個函數是 delete 掉,意思是我們不能直接調用這個構造函數來創建一個 expectedscalartypelongbut 對象。
2. expected(in_place_t, Args&&… args) 和 expected(in_place_t, std::initializer_list<U> ilist, Args&&… args)
template < typename... Args,
typename = std::enable_if_t<std::is_constructible_v<T, Args...>>>
explicit expected(in_place_t, Args&&... args) noexcept(std::is_nothrow_constructible_v<T, Args...>);
template<typename U, typename... Args,
typename = std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args...>>>
explicit expected(in_place_t, std::initializer_list<U> ilist, Args&&... args) noexcept(std::is_nothrow_constructible_v<T, std::initializer_list<U>&, Args...>);
我們分別看這兩個構造函數的聲明,這兩個構造函數的主要作用是用來通過參數直接構造 expectedscalartypelongbut 對象,而並非拷貝構造或者移動構造。其中,in_place_t 是一個空結構體,它的作用是表示使用in-place構造。對於第一個構造函數,我們需要傳入一系列的參數 Args,可以在 expectedscalartypelongbut 對象中對傳入的參數進行構造。而第二個構造函數需要先傳入一個std::initializer_list作為初始參數,之後我們可以訪問這個條件列表中的元素,同時再提供一些其他的參數 Args。
3. expected(const expected& rhs) 和 expected(expected&& rhs)
template<typename U = T,
typename = std::enable_if_t<std::is_copy_constructible_v<U>>>
expected(const expected& rhs);
template<typename U = T,
typename = std::enable_if_t<std::is_move_constructible_v<U>>>
expected(expected&& rhs) noexcept(std::is_nothrow_move_constructible_v<T>);
這兩個構造函數的作用是使用已有的 expectedscalartypelongbut 對象創建一個新的 expectedscalartypelongbut 對象。當我們調用拷貝構造函數時,內部會對 rhs 對象進行複製,而、調用移動構造函數時,則是將 rhs 的值全部轉移到新生成的 expectedscalartypelongbut 對象中。
三、expectedscalartypelongbut 的訪問函數以及異常處理
expectedscalartypelongbut 的核心作用是用來表示函數返回值中的錯誤信息,在我們使用時,除了前文提到的構造函數以外,還有一些訪問函數以及一些異常處理方式:
1. operator->() 和 operator*
這些函數的作用主要是方便我們訪問 expectedscalartypelongbut 對象內嵌的數據。比如,在我們需要使用expectedscalartypelongbut對象返回一個指針時,就可以通過operator->函數來訪問指針。而 operator*() 函數則是方便我們訪問 std::optional 中的數據。
2. operator bool() 和 has_value()
constexpr explicit operator bool() const noexcept;
constexpr bool has_value() const noexcept;
當 expectedscalartypelongbut 對象包含合法的數值時,而沒有錯誤的話,這兩個函數會返回 true。反之,如果 expectedscalartypelongbut 對象包含了一個 error() 信息,這兩個函數都會返回 false。
3. error() 和 map_error()
constexpr const E& error() const&;
constexpr E& error() &;
template<typename F>
expected<T, E>& map_error(F f);
當我們調用 error() 函數時,它會返回一個生成 expectedscalartypelongbut 對象的異常信息。如果 expectedscalartypelongbut 對象無錯誤,該函數將拋出異常,我們可以通過 map_error() 函數來處理。
map_error() 函數的作用是映射參數函數 f,如果 expectedscalartypelongbut 對象有異常信息,它會用 f 將異常信息映射為新的異常信息。這映射的過程也被稱作 functor 調用,它使用一個函數將異常信息傳入,同時返回新的異常信息。這一過程不改變當前對象的狀態,而只是返回映射後的新 expectedscalartypelongbut 對象。
四、expectedscalartypelongbut 的使用場景
expectedscalartypelongbut 在應用中被廣泛使用,特別是在函數的返回值中。當我們使用 expectedscalartypelongbut 時,我們應該優先考慮函數返回值中是否會存在錯誤。如果函數存在潛在的錯誤情況,我們就可以選擇使用 expectedscalartypelongbut 對象作為該函數的返回值。由於 expectedscalartypelongbut 對象可以在出現錯誤時,直接返回一個 error 信息,它因此可以幫助開發者更好的進行錯誤處理和處理異常情況。
五、總結
本文針對 expectedscalartypelongbut 這一模板的使用進行了詳細地介紹。我們分步介紹了 expectedscalartypelongbut 的定義、構造函數、訪問函數以及異常處理機制,並提到了其廣泛的應用場景。在使用 expectedscalartypelongbut 進行函數返回值時,可以通過它快速發現異常情況,並及時進行錯誤處理,因此它大大方便了函數調用和開發者的開發體驗。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/300909.html