为什么 GitHub C++ 核心指南说全局对象比单例更好?
Why GitHub C++ Core Guideline says that global object better than singleton?
The Github C++ Core Guidelines 说:
A global object is often better than a singleton.
我一直认为相反。从那时起,C++ 发生了什么变化?
或者这可能只是另一个错字?
这是避免同一个单例的理由guideline collection:
I.3: Avoid singletons
Reason
Singletons are basically complicated global objects in disguise.
Example
class Singleton {
// ... lots of stuff to ensure that only one Singleton object is created,
// that it is initialized properly, etc.
};
There are many variants of the singleton idea. That's part of the problem.
我对作者意图的分析:
越简单越好。如果在单例中伪装全局对象不能解决全局对象的问题——就像上面的指导方针所暗示的那样——那么通过伪装使代码复杂化是没有用的。
The Github C++ Core Guidelines 说:
A global object is often better than a singleton.
我一直认为相反。从那时起,C++ 发生了什么变化? 或者这可能只是另一个错字?
这是避免同一个单例的理由guideline collection:
I.3: Avoid singletons
Reason
Singletons are basically complicated global objects in disguise.
Example
class Singleton {
// ... lots of stuff to ensure that only one Singleton object is created,
// that it is initialized properly, etc.
};
There are many variants of the singleton idea. That's part of the problem.
我对作者意图的分析:
越简单越好。如果在单例中伪装全局对象不能解决全局对象的问题——就像上面的指导方针所暗示的那样——那么通过伪装使代码复杂化是没有用的。