Walter,

I would say that static functions are useful in two situations (in C++.)
One is, as you have indicated yourself, in relation to singletons. The
other is for meta-programming. I will only address the latter below.

The static qualifier is often used in C++ traits. Normally, traits are
used to "bundle" types (e.g. a container could have both an interator
and a const_iterator type) and behavior associated with types. The
behavior has to be declared as static functions, such as
char_traits<T>::length described in the quotation below.

The C++ std::string class, for instance, is really a typedef for the
basic_string<char> template (and the std::wstring uses the same template
but with the wchar_t type instead.)

 "Consider an example: when working with character strings, one common
  operation is to determine the length of a null terminated string.
  Clearly it's possible to write generic code that can do this, but it
  turns out that there are much more efficient methods available: for
  example, the C library functions strlen and wcslen  are usually
  written in assembler, and with suitable hardware support can be
  considerably faster than a generic version written in C++. The
  authors of the C++ standard library realized this, and abstracted
  the properties of char and wchar_t into the class char_traits.
  Generic code that works with character strings can simply use
  char_traits<>::length to determine the length of a null terminated
  string, safe in the knowledge that specializations of char_traits
  will use the most appropriate method available to them."

Quoted from:

http://www.boost.org/doc/libs/1_39_0/libs/type_traits/doc/html/boost_typetraits/background.html

This link contains a good description of C++ traits if you need more
information.

Reply via email to