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

前端 未结 9 670
时光取名叫无心
时光取名叫无心 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:19
    1. There are already some implementations of similar technologies for C++; e.g., Boehm-Demers-Weiser garbage collector.
    2. C++11 has a special Application Binary Interface for anyone wishing to add her own garbage collection.
    3. In the vast majority of cases, techniques like smart pointers can do the job of painless memory management for C++ developers.
    0 讨论(0)
  • 2021-02-04 02:27

    What's the advantage of using ARC rather than full garbage collection? There was a concrete proposal for garbage collection before the committee; in the end, it wasn't handled because of lack of time, but there seems to be a majority of the committee (if not truly a consensus) in favor of adding garbage collection to C++.

    Globally, reference counting is a poor substitute for true garbage collection: it's expensive in terms of run time, and it needs special code to handle cycles. It's applicable in specific limited cases, however, and C++ offers it via std::shared_ptr, at the request of the programmer, when he knows it's applicable.

    0 讨论(0)
  • 2021-02-04 02:34

    C++ has the concept of Resource Allocation is Initialization(RAII) & intelligent use of this method saves you from explicit resource management.

    C++ already provides shared_ptr which provides reference counting.

    Also, there are a host of other Smart pointers which employ RAII to make your life easier in C++.

    0 讨论(0)
提交回复
热议问题