was raw-pointer constructor of shared_ptr a mistake?

前端 未结 3 1548
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 02:57

In hindsight, given make_shared, would shared_ptr have a constructor that takes a raw pointer had it been introduced with C++11?

Are there stro

3条回答
  •  太阳男子
    2021-02-09 03:13

    std::shared_ptr does much more than allocate objects on the heap.

    Consider its use as an auto-closing shared file handle:

    #include 
    #include 
    
    
    int main()
    {
      auto closer = [](FILE* fp) { std::fclose(fp); };
      auto fp = std::shared_ptr(std::fopen("foo.txt", "r"),
                                      closer);
    }
    

提交回复
热议问题