LevelDB Explained - Preventing C++ Object Destruction
In the LevelDB source code, there’s a function for getting a Comparator that seemed a bit strange when I first saw it. It looks like it constructs a singleton, but it’s slightly more complex. The complete code is as follows:
1 | // util/comparator.cc |
Here, NoDestructor
is a template class that, judging by its name, is used to prevent object destruction. Why prevent object destruction, and how is it achieved? This article will delve into these questions.