本文介绍了为什么 C++ 没有垃圾收集器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之所以问这个问题,首先是因为垃圾收集的优点.我问这个的主要原因是我知道 Bjarne Stroustrup 说过 C++ 将在某个时间点有一个垃圾收集器.

I'm not asking this question because of the merits of garbage collection first of all. My main reason for asking this is that I do know that Bjarne Stroustrup has said that C++ will have a garbage collector at some point in time.

话虽如此,为什么没有添加?已经有一些用于 C++ 的垃圾收集器.这只是那些说起来容易做起来难"的事情之一吗?还是有其他原因没有添加(C++11不会添加)?

With that said, why hasn't it been added? There are already some garbage collectors for C++. Is this just one of those "easier said than done" type things? Or are there other reasons it hasn't been added (and won't be added in C++11)?

交叉链接:

澄清一下,我理解 C++ 最初创建时没有垃圾收集器的原因.我想知道为什么无法添加收集器.

Just to clarify, I understand the reasons why C++ didn't have a garbage collector when it was first created. I'm wondering why the collector can't be added in.

推荐答案

可以添加隐式垃圾回收,但它没有成功.可能不仅是因为实施的复杂性,还因为人们无法足够快地达成普遍共识.

Implicit garbage collection could have been added in, but it just didn't make the cut. Probably due to not just implementation complications, but also due to people not being able to come to a general consensus fast enough.

引用 Bjarne Stroustrup 本人的话:

A quote from Bjarne Stroustrup himself:

我曾希望有一个垃圾收集器可以选择启用将是 C++0x 的一部分,但有我有足够的技术问题凑合只是一个详细的这种收集器的规范与其余的集成语言(如果提供).情况是这样具有基本上所有 C++0x 特性,存在实验性实现.

here这里有很好的讨论.

There is a good discussion of the topic here.

一般概述:

C++ 非常强大,几乎可以让您做任何事情.出于这个原因,它不会自动将许多可能影响性能的东西推给您.垃圾回收可以很容易地用智能指针(用引用计数包装指针的对象,当引用计数达到 0 时自动删除自己)实现.

C++ is very powerful and allows you to do almost anything. For this reason it doesn't automatically push many things onto you that might impact performance. Garbage collection can be easily implemented with smart pointers (objects that wrap pointers with a reference count, which auto delete themselves when the reference count reaches 0).

C++ 在构建时考虑了没有垃圾收集的竞争对手.与 C 和其他语言相比,效率是 C++ 必须抵御批评的主要问题.

C++ was built with competitors in mind that did not have garbage collection. Efficiency was the main concern that C++ had to fend off criticism from in comparison to C and others.

有两种类型的垃圾回收...

There are 2 types of garbage collection...

显式垃圾回收:

C++0x 通过 shared_ptr 创建的指针进行垃圾回收

C++0x has garbage collection via pointers created with shared_ptr

如果你想要它,你可以使用它,如果你不想要它,你不会被强迫使用它.

If you want it you can use it, if you don't want it you aren't forced into using it.

对于 C++0x 之前的版本,boost:shared_ptr 存在并且用于相同的目的.

For versions before C++0x, boost:shared_ptr exists and serves the same purpose.

隐式垃圾回收:

虽然它没有透明的垃圾收集.不过,它将成为未来 C++ 规范的焦点.

It does not have transparent garbage collection though. It will be a focus point for future C++ specs though.

为什么 Tr1 没有隐式垃圾回收?

C++0x 的 tr1 应该有很多东西,Bjarne Stroustrup 在之前的采访中表示 tr1 没有他想要的那么多.

There are a lot of things that tr1 of C++0x should have had, Bjarne Stroustrup in previous interviews stated that tr1 didn't have as much as he would have liked.

这篇关于为什么 C++ 没有垃圾收集器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 19:52