詳解C++17中if和switch語句的新特性
1.從C++17開始,if語句允許在條件表達式里添加一條初始化語句。當僅在if語句范圍內需要變量時,使用這種形式的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不支持短路求值(當&&左側為false時停止求值,當||左側為true時停止求值)。
(5).if constexpr可以在任何函數(shù)中使用,而并非僅限于模版。只要條件表達式是編譯期的,并且可以轉換成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í)行結果如下圖所示:

GitHub:https://github.com/fengbingchun/Messy_Test
到此這篇關于詳解C++17中if和switch語句的新特性的文章就介紹到這了,更多相關C++17 if switch內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Opencv 馬賽克和毛玻璃效果與圖片融合的實現(xiàn)
這篇文章主要為大家詳細介紹了通過OpenCV實現(xiàn)馬賽克和毛玻璃濾鏡效果與圖片的融合,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11

