2

I found the following class in v8's source code:

// Superclass for classes only using static method functions.
// The subclass of AllStatic cannot be instantiated at all.
class AllStatic {
#ifdef DEBUG
 public:
  AllStatic() = delete;
#endif
};

Some questions:
1. why bother turning off the check when DEBUG is not defined? seems the compilation time impact is just marginal. Any other reasons?
2. I tried using the pattern(i.e. derive my pure utility class from AllStatic) in my own project. Even when debug, I don't feel any benefit except for readability in some cases. What is this pattern mainly for?

Edit: clarity what is the "pattern" in my question.

Leo Lai
  • 863
  • 8
  • 17
  • What "pattern" are you referring to? The non-default constructable class, or the `ifdef`? – juanchopanza Mar 14 '17 at 06:44
  • 1
    If a class has only static methods, then why worry about the possibility of creating its instances? – Zefick Mar 14 '17 at 06:47
  • 1
    See also http://stackoverflow.com/questions/1434937/namespace-functions-versus-static-methods-on-a-class – juanchopanza Mar 14 '17 at 06:48
  • 2) Why not just use a namespace instead? An "all statics" class can be useful as a traits type (usually templated), see [std::char_traits](http://en.cppreference.com/w/cpp/string/char_traits) or [std::numeric_limits](http://en.cppreference.com/w/cpp/types/numeric_limits) for example. – zett42 Mar 14 '17 at 07:04
  • 1
    Some people just love to create patterns when not really needed. Like using a singleton when you obviously just need a single instance, and wouldn't get the idea to create more than one anyway. Here someone seems to have created the "zeroton" pattern for no instances at all. Code using *lots* of classes with all static members, perhaps isn't the best model to learn C++ from? – Bo Persson Mar 14 '17 at 10:10
  • This pattern is for Java programmers who haven't noticed that C++ isn't Java. – Pete Becker Mar 14 '17 at 16:26
  • [Pete Becker](http://stackoverflow.com/users/1593860/pete-becker) could you comment why it makes sense in Java? – Leo Lai Mar 15 '17 at 02:08

0 Answers0