C++11 允许其实现执行(某些)垃圾收集实用程序。为什么标准会允许这样做?我一直有这样的印象:在 C++ 中,你不需要为不使用的东西付费。对我来说,(隐含的)GC 感觉它破坏了这种意识形态。此外,通过智能指针在 C++ 中编写和使用显式垃圾收集实用程序并不困难。
其次,GC会让一些原本有效的程序变得无效。示例包括指针屏蔽和相关的低级指针“黑客”。
int * nums = new int[10];
nums += 2;
*nums = 777; // nothing points to the new'ed int[10] at this point
// oh no! nums could have gotten collected!!! (so lets assume it was)
*nums = 666; // crash (or memory corruption (or something else that's bad))