欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解C++17中if和switch語句的新特性

 更新時間:2023年12月17日 09:56:56   作者:fengbingchun  
這篇文章主要為大家詳細(xì)介紹了C++17中if和switch語句的新特性的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.從C++17開始,if語句允許在條件表達式里添加一條初始化語句。當(dāng)僅在if語句范圍內(nèi)需要變量時,使用這種形式的if語句。在if語句的條件表達式里定義的變量將在整個if語句中有效,包括else部分。

std::mutex mx;
bool shared_flag = true; // guarded by mx
constexpr int get_value() { return 66; }
 
int test_if_init()
{
    std::map<std::string, std::string> addrs{
        {"csdn", "https://blog.csdn.net/fengbingchun/"},
        {"github", "https://github.com/fengbingchun"}
    };
 
    if (auto it = addrs.find("github"); it != addrs.end())
        std::cout << "github:" << it->second << "\n"; // github:https://github.com/fengbingchun
 
    if (auto it = addrs.find("csdn"); it == addrs.end())
        std::cout << "no find\n";
    else // if中的it變量在else分支中也有效
        std::cout << "csdn:" << it->second << "\n"; // csdn:https://blog.csdn.net/fengbingchun/
 
    if (auto [it, inserted] = addrs.insert({ "gitee", "https://gitee.com/fengbingchun/test.git" }); !inserted)
        std::cout << "already exists\n";
    else
        std::cout << "inserted successfully: key:" << it->first << ", value:" << it->second << "\n"; // inserted successfully: key:gitee, value:https://gitee.com/fengbingchun/test.git
 
    if (auto x = get_value(); x == 66)
        std::cout << "x is:" << x << "\n"; // x is:66
 
    if (std::lock_guard<std::mutex> lock(mx); shared_flag) {
        std::cout << "setting shared_flag to false\n"; // setting shared_flag to false
        shared_flag = false;
    }
 
    if (auto val1 = addrs.cbegin()->first, val2 = addrs.crbegin()->first; val1 != val2)
        std::cout << "val1:" << val1 << ", val2:" << val2 << "\n"; // val1:csdn, val2:github
 
    const std::string str{ "if" };
    if (auto keywords = { "if", "for", "while" }; std::any_of(keywords.begin(), keywords.end(), [&str](const char* kw) { return str == kw; }))
        std::cout << "Error:Token must not be a keyword\n"; // Error:Token must not be a keyword
 
    return 0;
}

2.從C++17開始,switch語句允許在條件表達式里添加一條初始化語句,其范圍僅限于switch語句塊。通過使用帶初始化的switch語句,我們可以在對條件表達式求值之前初始化一個對象/實體,用法與以上的if相同。

int test_switch_init()
{
    std::random_device rd;
    std::mt19937 mt(rd());
    std::uniform_int_distribution<int> dist(0, 100);
 
    switch (auto val = dist(mt); val) {
        default:
            std::cout << "val:" << val << "\n"; // val:20
    }
    
    return 0;
}

3.從C++17開始,你可以在函數(shù)模版中使用if constexpr語句做出編譯時分支決策,而無需使用(resort)多個函數(shù)重載.

if constexpr語句在編譯時求值,編譯器僅生成與發(fā)送到函數(shù)模板的參數(shù)類型相匹配的if分支的代碼。該功能主要用在模版中,它允許僅編譯特定的語句,具體取決于模版類型。

注意:

(1).if constexpr和if的唯一區(qū)別是:if constexpr在編譯時進行判斷,而if在運行時進行判斷;所以,使用if constexpr的代碼在編譯完成后,程序的這一部分其實就不會有分支存在。

(2).通過使用語法if constexpr,編譯器可以計算編譯期的條件表達式,在編譯期決定使用哪部分,其余部分的代碼將會被丟棄,但會進行語法檢查。所有的static_assert也必須有效,即使所在的分支沒有被編譯。

(3).不能在函數(shù)體之外使用if constexpr.

(4).if constexpr不支持短路求值(當(dāng)&&左側(cè)為false時停止求值,當(dāng)||左側(cè)為true時停止求值)。

(5).if constexpr可以在任何函數(shù)中使用,而并非僅限于模版。只要條件表達式是編譯期的,并且可以轉(zhuǎn)換成bool類型。

(6).在泛型代碼之外使用if constexpr的唯一好處是被丟棄的部分不會成為最終程序的一部分,這將減小生成的可執(zhí)行程序的大小。

template<typename T>
auto show(T t)
{
    //if (std::is_pointer_v<T>) // show(a) results in compiler error for return *t. show(p) results in compiler error for return t.
    if constexpr (std::is_pointer_v<T>) // this statement goes away for show(a)
        return *t;
    else
        return t;
}
 
template<typename T>
void print_value(const T& value)
{
    if constexpr (std::is_same_v<T, std::string>)
        std::cout << "type: std::string: value: " << value << ", length: " << value.length() << "\n";
    else if constexpr (std::is_same_v<T, int>)
        std::cout << "type: int: value: " << value << "\n";
    else if constexpr (std::is_same_v<T, float>)
        std::cout << "type: float: value: " << value << "\n";
    else
        std::cout << "unsupported type\n";
}
int test_if_constexpr()
{
    int a = 66;
    int* p = &a;
    std::cout << show(a) << "\n"; // 66
    std::cout << show(p) << "\n"; // 66
    std::string str{ "hello" };
    print_value(str); // type: std::string: value: hello, length: 5
    print_value(a); // type: int: value: 66
    float val{.6f };
    print_value(val); // type: float: value: 0.6
    print_value(p); // unsupported type
    return 0;
}

執(zhí)行結(jié)果如下圖所示:

GitHubhttps://github.com/fengbingchun/Messy_Test

到此這篇關(guān)于詳解C++17中if和switch語句的新特性的文章就介紹到這了,更多相關(guān)C++17 if switch內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Opencv 馬賽克和毛玻璃效果與圖片融合的實現(xiàn)

    Opencv 馬賽克和毛玻璃效果與圖片融合的實現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了通過OpenCV實現(xiàn)馬賽克和毛玻璃濾鏡效果與圖片的融合,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C語言#define拼接宏定義實現(xiàn)方式

    C語言#define拼接宏定義實現(xiàn)方式

    今天小編就為大家分享一篇C語言#define拼接宏定義實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 基于C語言實現(xiàn)簡單的掃雷小游戲

    基于C語言實現(xiàn)簡單的掃雷小游戲

    這篇文章主要為大家詳細(xì)介紹了基于C語言實現(xiàn)簡單的掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • C++中inline函數(shù)詳解

    C++中inline函數(shù)詳解

    inline函數(shù)的定義:在函數(shù)聲明或定義中函數(shù)返回類型前加上關(guān)鍵字inline,即可以把函數(shù)指定為內(nèi)聯(lián)函數(shù)。inline函數(shù)對編譯器而言必須是可見的,以便它能夠在調(diào)用點展開該函數(shù)。
    2015-07-07
  • C語言代碼實現(xiàn)掃雷小游戲

    C語言代碼實現(xiàn)掃雷小游戲

    這篇文章主要為大家詳細(xì)介紹了C語言代碼實現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C++ 封裝 DLL 供 C# 調(diào)用詳細(xì)介紹

    C++ 封裝 DLL 供 C# 調(diào)用詳細(xì)介紹

    這篇文章主要介紹了C++ 封裝 DLL 供 C# 調(diào)用(以C# 調(diào)用C++ 二次封裝的VLC播放庫為介質(zhì),支持回調(diào)函數(shù)的封裝),需要的朋友可以參考下面我文章的具體內(nèi)容
    2021-09-09
  • C++ 讓函數(shù)返回數(shù)組的方法

    C++ 讓函數(shù)返回數(shù)組的方法

    這篇文章主要介紹了C++ 讓函數(shù)返回數(shù)組的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 一文總結(jié)C++運算符的使用方法

    一文總結(jié)C++運算符的使用方法

    這篇文章主要為大家詳細(xì)總結(jié)了C++中運算符的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2023-05-05
  • C++快速排序及優(yōu)化方案詳解

    C++快速排序及優(yōu)化方案詳解

    這篇文章主要介紹了C++快速排序及優(yōu)化方案詳解,快速排序是一種常用的排序算法,它通過選擇一個基準(zhǔn)元素,將數(shù)組分成兩個子數(shù)組,其中一個子數(shù)組的所有元素都小于基準(zhǔn)元素,另一個子數(shù)組的所有元素都大于基準(zhǔn)元素,需要的朋友可以參考下
    2023-10-10
  • C語言關(guān)鍵字auto與register的深入理解

    C語言關(guān)鍵字auto與register的深入理解

    本篇文章是對c語言關(guān)鍵字auto與register的使用進行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05

最新評論