Why do C++ libraries and frameworks never use smart pointers?

后端 未结 8 724
时光取名叫无心
时光取名叫无心 2020-12-22 15:09

I read in a few articles that raw pointers should almost never be used. Instead they should always be wrapped inside smart pointers, whether it\'s scoped or shared pointers.

8条回答
  •  萌比男神i
    2020-12-22 15:57

    There are two issues with smart pointers (pre C++11):

    • non-standards, so each library tend to reinvent its own (NIH syndrom & dependencies issues)
    • potential cost

    The default smart pointer, in that it is cost-free, is unique_ptr. Unfortunately it requires C++11 move semantics, which only appeared recently. All other smart pointers have a cost (shared_ptr, intrusive_ptr) or have less than ideal semantics (auto_ptr).

    With C++11 around the corner, bringing a std::unique_ptr, one would be tempted to think that it is finally over... I am not so optimistic.

    Only a few major compilers implement most of C++11, and only in their recent versions. We can expect major libraries such as QT and Boost to be willing to retain compatibility with C++03 for a while, which somewhat precludes the wide adoption of the new and shiny smart pointers.

提交回复
热议问题