C++的optional用法實(shí)例詳解
optional用法
1 問題引出
編程中我們可能會(huì)遇到要處理可能為空的變量,比如說容器,基本類型,或者說對象實(shí)例,我們簡單看個(gè)例子:
#include <string>
#include <vector>
#include <memory>
struct Some
{
int some_i_ = 0;
std::string some_str_;
};
Some
getSome(const std::vector<Some>& svec,
int i)
{
auto iter =
std::find_if(svec.begin(), svec.end(),
[i](const Some& s) {
return s.some_i_ == i;
}
);
if (iter != svec.end()) {
return *iter;
}
return Some();
}
int main()
{
std::vector<Some> someVec;
someVec.push_back({1, "1"});
Some s = getSome(someVec, 1);
s = getSome(someVec, 2);
return 0;
}這里代碼很簡單,我們根據(jù)條件獲取vector中一個(gè)元素,這個(gè)元素是個(gè)結(jié)構(gòu)體,當(dāng)滿足條件時(shí)可以返回,但是沒有找到時(shí)仍然要返回一個(gè)對象,到我們main函數(shù)甚至要花一些力氣來判斷有沒有找到。如果沒有找到在getSome返回空就好了,這樣我們就來介紹optional
2 簡介
使用std::optional能夠達(dá)到上邊的效果,我們簡單了解下,首先optional是在c++17引入,可以看作是T類型和一個(gè)bool值的包裝。
關(guān)于std::optional可以接受對象或者nullopt(表示為空值),參考一段例子:
#include <iostream>
#include <optional>
using namespace std;
int main()
{
std::optional<int> pp = 1;
if (pp) {
cout << *pp << endl; // 1
}
pp = nullopt;
if (pp) {
cout << *pp << endl; // 不輸出
}
}我們看這個(gè)簡單的例子,pp用來存放int的對象,初始化為1,判斷pp是否包含值,可以輸出1,將nullopt賦值后,判斷時(shí)為false,自然也不會(huì)輸出。我們把上邊遺留的那個(gè)例子重新寫一下:
// snip...
#include <iostream>
using namespace std;
optional<Some>
getSome(const std::vector<Some>& svec, int i)
{
auto iter = std::find_if(svec.begin(), svec.end(), [i](const Some& s) {
return s.some_i_ == i;
});
if (iter != svec.end()) {
return *iter;
}
return nullopt;
}
int main()
{
vector<Some> someVec;
someVec.push_back({1, "11"});
auto s_ptr = getSome(someVec, 1);
if (s_ptr) {
cout << s_ptr->some_str_ << endl; // “11”
}
s_ptr = getSome(someVec, 2);
if (s_ptr) {
cout << s_ptr->some_str_ << endl; // 不輸出
}
return 0;
}我們把getSome的返回值的類型改為用optional包裝,如果滿足條件用Some對象填充,沒有時(shí)用nullopt填充,在main函數(shù)里判斷使用即可。
optional細(xì)則
創(chuàng)建optinal
有幾種方式創(chuàng)建optional,我們具體看下例子:
直接創(chuàng)建或者用nullopt賦值
std::optional<int> empty; std::optional<int> opt = std::nullopt;
使用對象初始化
std::optional<int> opt = 1;
struct Some
{
int some_i_ = 0;
std::string some_str_;
};
Some s;
std::optional<Some> opt = s;使用 std::make_optional構(gòu)造,類似std::make_shared可以傳遞參數(shù)原地構(gòu)造optional包含的對象
struct Some
{
Some(int i, std::string str):
some_i_(i),
some_str_(std::move(str)) {}
int some_i_ = 0;
std::string some_str_;
};
using namespace std;
optional<Some> opt = make_optional<Some>(1, "1");
auto opt = make_optional(1); // optional<int>使用std::in_place構(gòu)造:
其實(shí)使用std::in_place和使用std::make_optional 用法相近,都是原地構(gòu)造對象,避免使用對象初始化進(jìn)行的一次拷貝等。std::in_place只是一個(gè)tag,用來表示我們使用std::optional的那個(gè)構(gòu)造函數(shù)。
optional的構(gòu)造函數(shù)是這樣:
//
template <class... _Args, class = enable_if_t<
is_constructible_v<value_type, _Args...>>>
constexpr explicit
optional(in_place_t, _Args&&... __args)
: __base(in_place, _VSTD::forward<_Args>(__args)...) {}
//
template <class _Up, class... _Args, class = enable_if_t<
is_constructible_v<value_type, initializer_list<_Up>&, _Args...>>>
constexpr explicit
optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
: __base(in_place, __il, _VSTD::forward<_Args>(__args)...) {}
這里兩個(gè)構(gòu)造函數(shù)參數(shù)都是以in_place_t類型為第一個(gè)參數(shù),就是表示一個(gè)占位符,后邊我們傳入要構(gòu)造對象的參數(shù)。我們參考例子:
struct Some
{
Some(int i, std::string str):
some_i_(i),
some_str_(std::move(str)) {}
int some_i_ = 0;
std::string some_str_;
};
using namespace std;
optional<Some> opt {in_place, 1, "1"};寫起來要比std::make_optional簡便很多
optional的其他操作
/// 1
optional<int> opt {1};
opt.value(); // 1
*opt // 1
/// 2
optional<int> opt;
opt.value(); // 拋出異常
*opt // 為定義
opt.value_or(2); // 2(沒有值時(shí)使用默認(rèn)值)
///3
optional<int> opt{2};
opt.emplace(4); // 重新構(gòu)造4的對象
opt.reset(); // 釋放掉原來的對象,nulloptoptional比較
和指針比較
大家是否在想指針是不是也可以達(dá)到這樣的效果,我們來看一下:
- 如果我們和普通的指針相比,即用指針指向?qū)ο?,如果為空的時(shí)候使用nullptr來代替,對于我們第一個(gè)例子可以達(dá)到相似的效果,因?yàn)槲覀兊膙ector的生命周期時(shí)在使用指針之后銷毀,因?yàn)橹羔樦皇呛唵沃赶?,對于指向已?jīng)析構(gòu)的對象,無疑是一場災(zāi)難。
- 如果和我們智能指針比較,例如第一個(gè)例子中,
第一種實(shí)現(xiàn)我們需要vector存放shared_ptr才能進(jìn)行拷貝:
shared_ptr<Some> getSome(
const vector<shared_ptr<Some>>& svec,
int i)
{
auto iter = std::find_if(svec.begin(), svec.end(), [i](const Some& s) {
return s.some_i_ == i;
});
if (iter != svec.end()) {
return *iter;
}
return nullptr;
}實(shí)現(xiàn)起來有點(diǎn)繁瑣,并且還需要改動(dòng)svec,這不妥。或者看起來這樣:
shared_ptr<Some>
getSome(const vector<Some>& svec, int i)
{
auto iter = std::find_if(svec.begin(), svec.end(), [i](const Some& s) {
return s.some_i_ == i;
});
if (iter != svec.end()) {
Some s = *iter;
return shared_ptr<Some>{&s};
}
return nullptr;
}這樣就和我們使用普通指針是一樣的,并且shared_ptr引用計(jì)數(shù)為0的時(shí)候還是會(huì)做銷毀,這樣是錯(cuò)誤的。
最后一種就是重新構(gòu)造一個(gè)Some對象,普通指針和智能都可以實(shí)現(xiàn)。普通指針需要做delete操作,如果用智能指針實(shí)現(xiàn)也可以:
shared_ptr<Some>
getSome(const vector<Some>& svec, int i)
{
auto iter = std::find_if(svec.begin(), svec.end(),
[i](const Some& s) {
return s.some_i_ == i;
});
if (iter != svec.end()) {
return std::make_shared<Some>(*iter);
}
return nullptr;
}我們發(fā)現(xiàn)智能指針也可以充當(dāng)這樣的角色,如何使用要看大家了,不過既然推出了新的標(biāo)準(zhǔn),而且如果要實(shí)現(xiàn)如此功能感覺還是optional使用起來方便一點(diǎn),語義明確,而且代碼可讀性較好。
和rust的option比較
首先rust的option是一個(gè)枚舉:
enum Option<T> {
Some(T),
None,
} 這個(gè)枚舉是個(gè)模版,枚舉中每個(gè)元素可以存放對象或者不存放,類似之前例子的rust的簡單實(shí)現(xiàn):
fn getSome(b: bool) -> Option<i32> {
if b {
return Some(3);
}
return None;
}
fn main() {
let b = false;
if let Some(s) = getSome(b) {
println!("hello.. {}", s);
}
else {
println!("hello.. null");
}
}getSome如果滿足條件返回Some,不滿足返回None。
rust致力于一個(gè)安全的語言,option是prelude,不需要顯示引入作用域,同樣不需要Option::前綴來直接使用Some和None,同時(shí)還配套和一些相關(guān)安全的函數(shù),看起來比C++的簡便一些,我們這里就做一個(gè)對比。??
參考
https://en.cppreference.com/w/cpp/utility/optional/optional
https://kaisery.gitbooks.io/trpl-zh-cn/content/ch06-01-defining-an-enum.html
到此這篇關(guān)于C++的optional解析的文章就介紹到這了,更多相關(guān)C++ optional內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)視頻流轉(zhuǎn)換為圖片方式
今天小編就為大家分享一篇C++實(shí)現(xiàn)視頻流轉(zhuǎn)換為圖片方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
VS2019創(chuàng)建C++工程的的實(shí)現(xiàn)步驟
本文主要介紹了VS2019創(chuàng)建C++工程步驟,包含新建項(xiàng)目、編輯文件、配置源文件目錄、編譯鏈接、輸出文件、設(shè)置斷點(diǎn)調(diào)試,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
C語言詳細(xì)講解strcpy strcat strcmp函數(shù)的模擬實(shí)現(xiàn)
這篇文章主要介紹了怎樣用C語言模擬實(shí)現(xiàn)strcpy與strcat和strcmp函數(shù),strcpy()函數(shù)是C語言中的一個(gè)復(fù)制字符串的庫函數(shù),strcat()函數(shù)的功能是實(shí)現(xiàn)字符串的拼接,strcmp()函數(shù)作用是比較字符串str1和str2是否相同2022-05-05
Visual Studio Code上添加小程序自動(dòng)補(bǔ)全插件的操作方法
這篇文章主要介紹了Visual Studio Code上添加小程序自動(dòng)補(bǔ)全插件的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04

