Is it possible to introduce Automatic Reference Counting (ARC) to C++?

前端 未结 9 666
时光取名叫无心
时光取名叫无心 2021-02-04 01:54

Objective C has introduced a technology called ARC to free the developer from the burden of memory management. It sounds great, I think C++ developers would be very happy if g++

9条回答
  •  礼貌的吻别
    2021-02-04 02:09

    Recently I wrote some Objective-C++ code using Clang and was surprised to find that Objective-C pointers were actually handled as non-POD types in C++ that I could use in my C++ classes without issues.
    They were actually freed automatically in my destructors!
    I used this to store weak references in std::vectors because I couldn't think of a way to hold an NSArrary of weak references..
    Anyways, it seems to me like Clang implements ARC in Objective-C by emulating C++ RAII and smart pointers in Objective-C. When you think about it, every NSObject* in ARC is just a smart pointer (intrusive_ptr from Boost) in C++.
    The only difference I can see between ARC and smart pointers is that ARC is built into the language. They have the same semantics besides that.

提交回复
热议问题