Search code examples
c++global-variablesregistrationvoidcomma-operator

Global service registration and comma operator


There is a global function that registers services:

// services call this to register upon creation
extern void RegisterService(adapter::Service *s);

Then somewhere in a .cpp file some service registers itself:

// create the adapter and register with libecap to reach the host application
static const bool Registered = (libecap::RegisterService(new Adapter::Service), true);

Why registration isn't made simply by calling libecap::RegisterService(new Adapter::Service)? It looks even stranger, because the global variable Registered isn't used.


Solution

  • Because you can't place expressions in empty space floating around in a source file. Only declarations.

    This is a common way to force an expression to be evaluated here, even if the resulting object is never actually used afterwards.