看板 Marginalman 關於我們 聯絡資訊
為什麼要用 std::any? void* 是不安全的指標,只存儲記憶體地址而不帶有型別資訊。 你需要手動轉換型別(reinterpret_cast), 這很有可能會導致型別錯誤,而且不容易追蹤。 std::any 則是型別安全的, 可以存儲任意型別的值,並在執行時檢查型別, 避免了錯誤的轉型操作,提升程式的安全性。 void*的情況 Code: #include <iostream> void printValue(void* ptr) { std::cout << *reinterpret_cast<int*>(ptr) << std::endl; // 需要轉型 } int main() { int value = 42; printValue(&value); // 必須傳指標 return 0; } std::any的情況 Code: #include <any> #include <iostream> void printValue(const std::any& value) { std::cout << std::any_cast<int>(value) << std::endl; // 型別安全 } int main() { std::any value = 42; // 可以存放任意型別的值 printValue(value); // 無需手動轉型 return 0; } 並且std::any可以型別檢查: if (value.type() == typeid(int)) {...} 也可以偵測錯誤轉換: try { std::cout << std::any_cast<int>(value) << std::endl; // 嘗試轉換為int } catch (const std::bad_any_cast& e) { std::cerr << "Error: " << e.what() << std::endl; // 捕獲轉換錯誤 // Error: bad any_cast } 表格: 特性 void* std::any 型別安全 否,可能導致未定義行為 是,提供型別檢查與安全轉換 使用簡單性 要手動追蹤型別,不方便 不需要追蹤型別,自動管理 動態型別支持 基本支持,但容易出錯 完整支持,型別檢查更嚴格 效能 較快,但風險更高 較慢,但更安全 適用場景 C 接口、低層次操作 Modern C++ 更推薦 點評: std::any不是任何情況都能取代void*, 但在能取代的場合更好用。 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.143.163 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1730113344.A.A49.html
sustainer123: 大師 10/28 19:02
yam276: 恨void* 10/28 19:03
sustainer123: 話說C++有推薦的課或書ㄇ 最近想學 10/28 19:03
Meaverzt: 大師 10/28 19:04
yam276: C++最好的方式就是自己被荼毒一遍 因為狗屎太多了 10/28 19:04
yam276: 你嘗試用raw pointer搞一個複雜一點的專案就知道坑在哪 10/28 19:04
IGZX: 推專業 10/28 19:05
Ceelo: 我被指標整死了 10/28 19:06
yam276: 很多學其他高階語言起家的分不清傳值傳址 10/28 19:07
sustainer123: 唉 恨指標 最近複習C看到指標了 10/28 19:07
wu10200512: 我逃去python了 謝謝 10/28 19:10
sustainer123: 你不是寫pixel 怎逃py 10/28 19:12
ILoveErr: 大師 10/28 19:14
curance: python好玩捏 10/29 12:05