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.


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.

The aithor of the code wanted to have a bool flag that will say whether the service already registered. He could write for example

libecap::RegisterService(new Adapter::Service);
static const bool Registered =  true;

provided that Registered is a local variable.

But if Registered is not a local variable then he may not call a function outside some other function (or main). So the only method to define a global bool variable (with external or internal linkage) and at the same time to call a function that has return type void is the following

static const bool Registered = (libecap::RegisterService(new Adapter::Service), true);

The result of the expression of the comma operator is true an Registered will be initialized by this value. And at the same time function libecap::RegisterService(new Adapter::Service) will be called.

As an alternative he could define a class the constructor of which would contain the call of the function and the destructor would contain some unregister stuff.

Need Your Help

Android sqlite query for string

android sqlite

Cursor cursor = db.rawQuery("SELECT id,lastname FROM people WHERE lastname='John Kenedy'; ",null);

NullReference Exception is thrown while getting a callback channel

wcf callback nullreferenceexception duplex contract

I am trying to get along with WCF's duplex contracts. A code from this article