type_info::before的用途

Compare order of types
Returns whether the type precedes the type identified by rhs in some implementation-specific order.

這是cplusplus.com 網站給的解釋

bool before(const type_info& rhs) const;
Effects: Compares the current object with rhs.
Returns: true if *this precedes rhs in the implementation’s collation order.

這是ISO IEC14882 1998 C++標準給的解釋

不過兩種解釋我都沒看懂,到底這個函數是幹啥用的

最終再stackover上找到了答案:就是在我們希望將type_info類型的數據放入容器時,如何排序,誰前誰後,比如我們需要操作形如std::map<type_info*, value>這種結構時,我們並不能直接比較type_info指針的地址或者name(),因爲他們可能對應多個值(當我們使用動態鏈接庫,即不同的實現時),before()就派上了用場。

C++真是博大精深,感覺自己還只是瞭解了皮毛-_-!

發佈了25 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章