std::shared_ptr
的推導指引
來自 cppreference.com
< cpp | 記憶體 | shared ptr
定義於標頭檔案 <memory> |
||
template< class T > shared_ptr( std::weak_ptr<T> ) -> shared_ptr<T>; |
(1) | (C++17 起) |
template< class T, class D > shared_ptr( std::unique_ptr<T, D> ) -> shared_ptr<T>; |
(2) | (C++17 起) |
為 std::shared_ptr 提供了這些推導指引,以處理隱式推導指引遺漏的邊緣情況。
注意,不存在從指標型別進行的類模板實參推導,因為無法區分從陣列和非陣列形式的 new 獲得的指標。
[編輯] 示例
執行此程式碼
#include <memory> int main() { auto p = std::make_shared<int>(42); std::weak_ptr w{p}; // explicit deduction guide is used in this case std::shared_ptr p2{w}; // explicit deduction guide is used in this case }