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

后端 未结 8 733
时光取名叫无心
时光取名叫无心 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条回答
  •  天命终不由人
    2020-12-22 15:39

    There are also other types of smart pointers. You might want a specialized smart pointer for something like network replication (one that detects if it's accessed and sends any modifications to the server or some such), keeps a history of changes, marks the fact that it was accessed so it can be investigated when you save data to disk and so on. Not sure if doing that in the pointer is the best solution but using the built in smart pointer types in libraries could result in people being locked into them and loosing the flexibility.

    People can have all kinds of different memory management requirements and solutions beyond smart pointers. I might want to manage memory myself, I could be allocating space for things in a memory pool so it's allocated in advance and not at runtime (useful for games). I might be using a garbage collected implementation of C++ (C++11 makes this possible although none exist yet). Or maybe I'm just not doing anything advanced enough to worry about bothering with them, I can know that I'm not going to forget to uninitialized objects and so on. Maybe I'm just confident in my ability to manage memory without the pointer crutch.

    Integration with C is another issue too.

    Another issue is smart pointers are part of the STL. C++ is designed to be usable without the STL.

提交回复
热议问题