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

问题描述

在中有:

(但似乎没有在GCC和Clang中实现。)

(but it seems not implemented in either GCC and Clang.)

为什么标准委员会引入了这种垃圾收集C ++ langauge功能?

Why the standard committee introduced this garbage collection C++ langauge feature?

C ++真的需要一个GC吗?是不是RAII这样一个优秀的模式(可以统一用于内存和非内存资源,如套接字,文件,纹理...)?

Does C++ really need a GC? Isn't RAII such an excellent pattern (that can be used uniformly for both memory and non-memory resources, like sockets, files, textures...)?

一个GC破坏了使用RAII的C ++代码模式的一致性?

Will a GC break the uniformity of C++ code pattern that uses RAII?

有些人说GC可以派上用场来打破循环依赖,但不是只是OK为此目的使用 weak_ptr 这样的智能指针?

Some people say that a GC can come in handy to break circular dependencies, but isn't it just OK to use smart pointers like weak_ptr for this purpose?

在抛出异常时会发生什么?

And what happens in case of exceptions thrown? How will the stack unwind semantics be modified to take a GC into consideration?

并且将一个C#类 IDisposable 模式也被引入?

And will a C#-like IDisposable pattern be introduced as well?

此外,假设在C ++中引入了GC,指针语法会不同吗?例如我们会像C ++ / CLI或C ++ / CX扩展中一样有一些像帽子一样的指针 ^ 吗?应该有一种区别于普通原始指针与托管指针的方法,对吗?

Moreover, assuming that a GC is introduced in C++, will the pointer syntax be different? e.g. Will we have some hat-like "pointers" ^ like in C++/CLI or C++/CX extensions? There should be a way to differentiate from ordinary raw pointers vs. "managed" pointers, right?

推荐答案

引入一个垃圾收集器 - 它只是允许它在某些情况下,如果实现选择。标准将仅仅将这些情况描述为引起未定义的行为。在这样做的时候,它放松了实现的要求,为垃圾收集器提供了最小的余地。

The proposal doesn't introduce a garbage collector - it just allows for it in certain situations if the implementation chooses. The standard will just describe these situations as causing undefined behaviour. In doing this, it relaxes the requirements of the implementation, giving the minimal leeway for a garbage collector.

在考虑何时获取指向动态分配对象的指针,将其与另一个值进行异或,隐藏指针值,然后恢复原始指针值以通过它访问对象。在C ++ 11之前,这将是完美的,它仍然是有效的使用。然而,现在这样的操作可能(参见下一段)被认为是未定义的行为,这意味着实现可以对指向的对象执行垃圾收集。

The simple example given in the proposal considers when you take a pointer to a dynamically allocated object, XOR it with another value, thereby hiding the pointer value, and then recover the original pointer value to access the object through it. Before C++11, this would be perfectly fine and it would still be valid to use. However, now such an operation may be (see next paragraph) considered undefined behaviour, which means that an implementation may do garbage collection on the object that was pointed to.

标准规定,实现可以具有放宽指针安全,在这种情况下行为与以前一样,或严格指针安全允许引入一个垃圾收集器。

The standard states that an implementation can either have relaxed pointer safety, in which case the behaviour is as it was before, or strict pointer safety, which allows for the introduction of a garbage collector.

指针值一个安全派生的指针值,如果它指向一个动态分配的对象,并没有发生任何有趣的事情(更具体地定义在§3.7.4.3)。

A pointer value is a safely-derived pointer value if it points at a dynamically allocated object and hasn't had any funny business happen to it (defined more specifically in §3.7.4.3).

如果你的实现有严格的指针安全,但你仍然想做一个有趣的业务到一个指针,而不引入未定义的行为,你可以声明一个指针 p 为可达到像这样:

If your implementation has strict pointer safety yet you still want to do said funny business to a pointer without introducing undefined behaviour, you can declare a pointer p as being reachable like so:

declare_reachable(p);

此函数在< memory> 头,以及诸如 undeclare_reachable declare_no_pointers undeclare_no_pointers 。您还可以使用 get_pointer_safety 确定实施的严格性。

This function is defined in the <memory> header, along with related functions such as undeclare_reachable, declare_no_pointers, and undeclare_no_pointers. You can also determine the strictness of your implementation using get_pointer_safety.

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

09-18 06:28