C++17之std::any的具體使用
一般來說,c++是一種具有類型綁定和類型安全性的語言。值對象聲明為具有特定類型,該類型定義哪些操作是可能的以及它們的行為方式。值對象不能改變它們的類型。
std: any是一種值類型,它能夠更改其類型,同時仍然具有類型安全性。也就是說,對象可以保存任意類型的值,但是它們知道當前保存的值是哪種類型。在聲明此類型的對象時,不需要指定可能的類型。
訣竅在于,對象同時擁有包含的值和使用typeid包含值的類型。因為這個值可以有任何大小,所以可以在堆上分配內存,鼓勵實現(xiàn)避免小對象的動態(tài)分配。也就是說,如果分配一個字符串,對象將為該值分配內存并復制該字符串,同時也在內部存儲分配的字符串。稍后,可以執(zhí)行運行時檢查來確定當前值的類型,并使用any_cast<該值的類型>獲取值。
1. 使用std::any
下面的例子演示了std::any:
std::any a; // a is empty std::any b = 4.3; // b has value 4.3 of type double a = 42; // a has value 42 of type int b = std::string{"hi"}; // b has value "hi" of type std::string if (a.type() == typeid(std::string)) { std::string s = std::any_cast<std::string>(a); useString(s); } else if (a.type() == typeid(int)) { useInt(std::any_cast<int>(a)); }
可以聲明std::any為空或由特定類型的值初始化。初始值的類型成為所包含值的類型。通過使用成員函數(shù)type(),可以根據(jù)任何類型的類型ID檢查所包含值的類型。如果對象是空的,對象類型ID是typeid(void)。要訪問包含的值,可以通過std::any_cast<對象類型>的方式:
auto s = std::any_cast<std::string>(a);
如果轉換失敗,因為對象為空或包含的類型不匹配,則拋出std::bad_any_cast。因此,在不檢查或不知道類型的情況下,最好實現(xiàn)以下功能:
try { auto s = std::any_cast<std::string>(a); ... } catch (std::bad_any_cast& e) { std::cerr << "EXCEPTION: " << e.what() << '\n'; }
注意,std::any_cast<>創(chuàng)建了一個傳遞類型的對象。如果將std::string作為模板參數(shù)傳遞給std::any_cast<>,它將創(chuàng)建一個臨時string(一個prvalue),然后用它初始化新對象s。如果沒有這樣的初始化,通常最好轉換為引用類型,以避免創(chuàng)建臨時對象:
std::cout << std::any_cast<const std::string&>(a);
要修改該值,需要轉換為對應的引用類型:
std::any_cast<std::string&>(a) = "world";
還可以為std::any對象的地址調用std::any_cast。在這種情況下,如果類型匹配,則強制轉換返回相應的地址指針;如果不匹配,則返回nullptr:
auto p = std::any_cast<std::string>(&a); if (p) { ... }
例1:
#include <iostream> #include <any> int main() { std::any i = 42; const auto ptr = std::any_cast<int>(&i); if (ptr) { std::cout << ptr << std::endl; } return 0; }
結果如下:
要清空現(xiàn)有std::任何可以調用的對象:
方法1:a.reset(); // makes it empty
方法2:a = std::any{};
方法3: a = {};
可以直接檢查對象是否為空:
if (a.has_value()) { ... }
還要注意,值是使用衰減類型存儲的(數(shù)組轉換為指針,忽略頂層引用和const)。對于字符串常量,這意味著值類型是const char*。要檢查type()并使用std::any_cast<>,必須使用以下類型:
std::any a = "hello"; // type() is const char* if (a.type() == typeid(const char*)) { // true ... } if (a.type() == typeid(std::string)) { // false ... } std::cout << std::any_cast<const char*>(v[1]) << '\n'; // OK std::cout << std::any_cast<std::string>(v[1]) << '\n'; // EXCEPTION
std::any沒有定義比較運算符(因此,不能比較或排序對象),沒有定義hash函數(shù),也沒有定義value()成員函數(shù)。由于類型只在運行時才知道,所以不能使用泛型lambdas處理與類型無關的當前值。總是需要運行時函數(shù)std::any_cast<>來處理當前值。
然而,可以將std::任何對象放入容器中。
例2:
#include <iostream> #include <vector> #include <any> int main() { std::vector<std::any> v; v.push_back(42); std::string s = "hello"; v.push_back(s); for (const auto& a : v) { if (a.type() == typeid(std::string)) { std::cout << "string: " << std::any_cast<const std::string&>(a) << '\n'; } else if (a.type() == typeid(int)) { std::cout << "int: " << std::any_cast<int>(a) << '\n'; } } }
結果如下:
2. std::any類型和操作
本節(jié)詳細描述std::any的類型和操作。
2.1 std::any的類型
在頭文件<any>中,c++標準庫定義了類std::any,如下所示:
namespace std { class any; }
也就是說,std::any根本不是類模板。
此外,定義了以下類型和對象:
如果類型轉換失敗,則拋出異常類型std::bad_any_cast,它派生自std::bad_cast,而std::bad_cast派生自std::exception。
any對象還可以使用<utility>中定義的對象std::in_place_type(類型為std::in_place_type_t)。
2.2 std::any操作
std::any操作
函數(shù) | 說明 |
constructors | 創(chuàng)建一個any對象(可能調用底層類型的構造函數(shù)) |
make_any() | 創(chuàng)建一個any對象(傳遞值來初始化它) |
destructor | 銷毀any對象 |
= | 分配一個新值 |
emplace<T>() | 分配一個類型為T的新值 |
reset() | 銷毀any對象的值(使對象為空) |
has_value() | 返回對象是否具有值 |
type() | 返回當前類型為std::type_info對象 |
any_cast<T>() | 使用當前值作為類型T的值(如果其他類型除外) |
swap() | 交換兩個any對象的值 |
1. 構造函數(shù)
默認情況下,std::any的初始值為空。
std::any a1; // a1 is empty
如果傳遞一個值進行初始化,則將其衰減類型用作所包含值的類型:
std::any a2 = 42; // a2 contains value of type int std::any a3 = "hello"; // a2 contains value of type const char*
要保存與初始值類型不同的類型,必須使用in_place_type標記:
std::any a4{std::in_place_type<long>, 42}; std::any a5{std::in_place_type<std::string>, "hello"};
即使傳遞給in_place_type的類型也會衰減。下面的聲明包含一個const char*:
std::any a5b{std::in_place_type<const char[6]>, "hello"};
要通過多個參數(shù)初始化可選對象,必須創(chuàng)建該對象或將std::in_place_type添加為第一個參數(shù)(不能推斷包含的類型):
std::any a6{std::complex{3.0, 4.0}}; std::any a7{std::in_place_type<std::complex<double>>, 3.0, 4.0};
甚至可以傳遞一個初始化器列表,后面跟著附加的參數(shù):
// initialize a std::any with a set with lambda as sorting criterion: auto sc = [] (int x, int y) { return std::abs(x) < std::abs(y);}; std::any a8{std::in_place_type<std::set<int,decltype(sc)>>, {4, 8, -7, -2, 0, 5}, sc};
注意,還有一個方便的函數(shù)make_any<>(),它可以用于單個或多個參數(shù)(不需要in_place_type參數(shù))。必須顯式指定初始化的類型(如果只傳遞一個參數(shù),則不會推導出初始化的類型):
auto a10 = std::make_any<float>(3.0); auto a11 = std::make_any<std::string>("hello"); auto a13 = std::make_any<std::complex<double>>(3.0, 4.0); auto a14 = std::make_any<std::set<int,decltype(sc)>>({4, 8, -7, -2, 0, 5}, sc);
2. 訪問值
要訪問包含的值,必須使用std::any_cast<>將其轉換為其類型。將該值轉換為一個字符串,有幾個選項:
std::any_cast<std::string>(a) // yield copy of the value std::any_cast<std::string&>(a); // write value by reference std::any_cast<const std::string&>(a); // read-access by reference
在這里,如果轉換失敗,將拋出std::bad_any_cast異常。
如果把std::any中所包含的類型轉換為移除了傳遞類型的頂層引用后的類型ID,則轉換類型是適合的。如下:
#include <iostream> #include <string> #include <any> int main() { const auto& s = std::make_any<std::string>("hello"); if (s.type() == typeid(std::string))//刪除頂層cosnt和引用后的類型 { auto a = std::any_cast<std::string>(s); std::cout << a << std::endl; } return 0; }
結果如下:
如果類型不匹配轉換失敗了,傳遞一個地址將會返回nullptr:
auto a = std::make_any<std::string>("hello"); std::any_cast<std::string>(&a) // write-access via pointer std::any_cast<const std::string>(&a); // read-access via pointer
注意,這里轉換到引用會導致運行時錯誤:
std::any_cast<std::string&>(&a); // RUN-TIME ERROR
3. 修改值
相應的賦值和emplace()操作。例如:
#include <iostream> #include <string> #include <any> #include <complex> int main() { std::any a; a = 42; // a contains value of type int a = "hello"; // a contains value of type const char* a.emplace<std::string>("hello world");// a contains value of type std::string return 0; }
結果如下:
4. 移動語法
std: any也支持移動語義。但是,請注意,move語義必須滿足包含的類型具有可復制構造函數(shù)。也就是說,不支持只移動類型作為包含值類型。處理move語義的最佳方法可能并不明顯。所以,你應該這樣做:
std::string s("hello, world!"); std::any a; a = std::move(s); // move s into a s = std::move(std::any_cast<string&>(a)); // move assign string in a to s
與通常的從對象移動的情況一樣,在最后一次調用之后,所包含的值a是未指定的。因此,可以使用a作為字符串,只要沒有對所包含的字符串值的值做任何假設。
注意:
s = std::any_cast<string>(std::move(a));
也可以,但需要一個額外的移動。然而,以下內容是危險的(盡管它是c++標準中的一個例子):
std::any_cast<string&>(a) = std::move(s2); // OOPS: a to hold a string
只有當包含的值已經是字符串時,才可以這樣做。如果沒有,轉換將拋出一個std::bad_any_cast異常。
到此這篇關于C++17之std::any的具體使用的文章就介紹到這了,更多相關C++17 std::any內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Windows 環(huán)境下使用 Qt 連接 MySQL
這篇文章主要介紹了Windows 環(huán)境下使用 Qt 連接 MySQL的相關資料,需要的朋友可以參考下2017-07-07OpenCV提取圖像中圓線上的數(shù)據(jù)具體流程
在對圖像進行處理時,經常會要提取出圖像中某條直線、圓線或者ROI區(qū)域內的感興趣數(shù)據(jù),進行重點關注。本文主要介紹了利用OpenCV獲取圖像中圓線上的數(shù)據(jù),需要的可以參考一下2021-11-11C/C++ 運用Npcap發(fā)送UDP數(shù)據(jù)包的完美過程
UDP 是一種無連接、輕量級的傳輸層協(xié)議,與 TCP 相比,它不提供可靠性、流控制和錯誤恢復機制,但卻更加簡單且具有較低的開銷,這篇文章主要介紹了C/C++ 運用Npcap發(fā)送UDP數(shù)據(jù)包,需要的朋友可以參考下2023-11-11