Re: C++: conditional static data member possible?



Carlos Moreno <moreno_at_mochima_dot_com@xxxxxxxxxxxxxx> writes:

Here's the puzzle I'm trying to solve:

Say that I have a shared library with, among other things,
a class A of which an application may or may not
instantiate objects.

I'm trying to enforce the guarantee that by the end of the
application, at least one object of class A has executed a
given action.

That part was easy: static data member that acts like a
flag --- the given action (which is one of class A methods)
notifies that static data member to that the flag is set.
That static object is implemented through a class whose
destructor verifies that the action has taken place, and
if not, it logs an error.

Now, the real puzzle I'm facing: if the application does
not *declare* a single object of class A, I do not want to
enforce anything (i.e., I do not want to report/log an
error because no object of class A has executed the action).

Example:

int main()
{
B b;
b.something();
C c;
c.something();

return 0;
}

In the above code, I do not want A's static data member to
notify an error message, since class A does not appear
anywhere in the code. However, in the code below, I would
like to report the error:

int main()
{
if (some condition that is false)

if (globalA.referenced() && false condition)

{
// IOW, this code does not execute
A a;
a.the_required_action();
}
else
{
B b;
b.something();
}

return 0;
}

Notice that even though *no object of class A is instantiated*
in the above code, the mere fact that there *are* declarations
in the code for objects of class A determines that I want the
application to report the error.

Why do you want this?


Is this possible with g++ ? Ideally, everything should be
compiled to a single shared object (.so file).

Thanks,

Carlos

--
.