亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++使用expected實現(xiàn)優(yōu)雅的錯誤處理

 更新時間:2023年06月13日 10:29:08   作者:L_B__  
C++ 中提供了很多中方式進(jìn)行錯誤處理。無論是通過拋異常還是通過錯誤碼,標(biāo)準(zhǔn)庫都提供相應(yīng)的調(diào)用,今天本文為大家介紹的是使用expected進(jìn)行錯誤處理,感興趣的可以了解一下

使用expected進(jìn)行錯誤處理

C++ 中提供了很多中方式進(jìn)行錯誤處理。無論是通過拋異常還是通過錯誤碼,標(biāo)準(zhǔn)庫都提供相應(yīng)的調(diào)用。

  • 通過 try catch 以拋出異常的方式進(jìn)行處理,這種方式很多人會覺得看起來可讀性很差(包括我),并且由于缺少異常規(guī)約(某些異常必須捕獲),容易出現(xiàn) bug,而且異常的傳遞很多時候可能伴隨動態(tài)分配內(nèi)存,這是一筆不小的開銷。
  • 通過 error_code 作為返回值判斷,這種方式雖然看似簡單易用,但是由于 C++ 中并未對 error_code 作過多的規(guī)范,使用起來并不方便,很多時候還是傾向于自定義一些枚舉作為自己的 error_code,但是由于缺少多返回值的語法(當(dāng)然如果C++版本比較高可用使用tuple以及解構(gòu)的語法實現(xiàn)),如果把錯誤碼作為返回值,那么原本的數(shù)據(jù)返回就只能函數(shù)參數(shù)傳遞引用的形式返回了。當(dāng)然,如果不考慮函數(shù)返回值的具體錯誤信息,可以使用 C++17 的 optional 。

由于 optional 無法包含具體的錯誤信息,expected 橫空出世,在 C++23 開始納入標(biāo)準(zhǔn)。如果你的C++版本較低,可以使用第三方開源的 expected 庫:github.com/TartanLlama/expected

下面我會以一個例子把第三方庫中的 expected 庫的使用方式介紹給大家。

expected 使用實例

由于該第三方庫是 head-only 的,所以你只需要進(jìn)到GitHub倉庫把對應(yīng)的頭文件復(fù)制過來,便可引入使用。

下面是示例代碼,樣例是 cppreference 中的。

#include "expected.h"
#include <iomanip>
#include <iostream>
#include <string>
?
enum class parse_error
{
    invalid_input,
    overflow
};
?
tl::expected<double, parse_error> parse_number(std::string_view& str)
{
    const char* begin = str.data();
    char* end;
    double retval = std::strtod(begin, &end);
?
    if (begin == end)
        return tl::unexpected(parse_error::invalid_input);
    else if (std::isinf(retval))
        return tl::unexpected(parse_error::overflow);
?
    str.remove_prefix(end - begin);
    return retval;
}
?
int main()
{
    auto process = [](std::string_view str) {
        std::cout << "str: " << std::quoted(str) << ", ";
        if (const auto num = parse_number(str); num)
        {
            std::cout << "value: " << *num << '\n';
            // If num did not have a value, dereferencing num
            // would cause an undefined behavior, and
            // num.value() would throw std::bad_expected_access.
            // num.value_or(123) uses specified default value 123.
        }
        else if (num.error() == parse_error::invalid_input)
        {
            std::cout << "error: invalid input\n";
        }
        else if (num.error() == parse_error::overflow)
        {
            std::cout << "error: overflow\n";
        }
        else
        {
            std::cout << "unexpected!\n";// or invoke std::unreachable();
        }
    };
?
    for (auto src : {"42", "42abc", "meow", "inf"})
        process(src);
}

上面的代碼如果想要跑通,情確保C++版本至少是C++17,因為其中用到了 string_view 以及更智能的自動類型推導(dǎo)(如果低于這個幫會導(dǎo)致unexpected需要指定明確的error類型)。

函數(shù)式的接口

  • and_then:傳入一個回調(diào),在沒有錯誤的時候調(diào)用,該回調(diào)的返回值是新的 expected 值(可以控制err)。如果有錯誤返回原 expected 值。
  • or_else:傳入一個回調(diào),在有錯誤的時候調(diào)用,該回調(diào)的返回值是新的 expected 值(可以控制err),并且回調(diào)的參數(shù)是對應(yīng)的錯誤類型。如果沒有錯誤返回原 expected 值。
  • transform/map:transform 是C++23標(biāo)準(zhǔn)中規(guī)定的接口,而該第三方庫作者又實現(xiàn)了一個名為map的接口,這兩者效果是一致的。傳入一個回調(diào),在沒有錯誤的時候調(diào)用,回調(diào)的參數(shù)和返回值都不牽扯 expected 值,只是作值的變換,所以無法控制新的 expected 的 err 值。如果有錯誤則返回原 expected 值。
  • transform_error/map_error:同上,但回調(diào)的調(diào)用時機(jī)和參數(shù)于 or_else 相同,但是需要注意的是,回調(diào)的返回值并不具備任何效用,也就是說如果 transform_error 中的回調(diào)被調(diào)用,那么返回的仍然是原本包含錯誤信息的 expected 值。

簡單示例如下:

#include "expected.h"
#include <iostream>
#include <string>
?
enum class parse_error
{
    invalid_input,
    overflow
};
?
tl::expected<double, parse_error> parse_number(std::string_view& str)
{
    const char* begin = str.data();
    char* end;
    double retval = std::strtod(begin, &end);
?
    if (begin == end)
        return tl::unexpected(parse_error::invalid_input);
    else if (std::isinf(retval))
        return tl::unexpected(parse_error::overflow);
?
    str.remove_prefix(end - begin);
    return retval;
}
?
int main()
{
    auto sv = std::string_view{"0"};
    auto result = parse_number(sv)
                      .and_then([](double x) {
                         ?return tl::expected<double, parse_error>(x + 1);
                      })
                      .map([](double x) {
                         ?return x + 1;
                      })
                      .transform([](double x) {
                         ?return x + 1;
                      });
    if (result)
        std::cout << *result << "\n";
    auto result2 = parse_number(sv)
                     ? .and_then([](double x) {
                         ? //自己構(gòu)造了一個錯誤
                         ? tl::expected<double, parse_error> ret = tl::unexpected<parse_error>(parse_error::invalid_input);
                         ? return ret;
                     ? })
                     ? .or_else([](parse_error err) {
                         ? if (err == parse_error::invalid_input)
                         ? {
                             ? std::cout << "invalid error\n";
                         ? }
                         ? //自己構(gòu)造了一個錯誤
                         ? tl::expected<double, parse_error> ret = tl::unexpected<parse_error>(parse_error::overflow);
                         ? return ret;
                     ? })
                     ? .transform_error([](parse_error err) {
                         ? if (err == parse_error::overflow)
                         ? {
                             ? std::cout << "overflow error\n";
                         ? }
                         ? return 32432.4324;
                     ? }).map([](double x){
                         ? return x+1;
                     ? });
    if (result2)
    {
        std::cout << *result2;
    }
}

到此這篇關(guān)于C++使用expected實現(xiàn)優(yōu)雅的錯誤處理的文章就介紹到這了,更多相關(guān)C++錯誤處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論