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



Carlos Moreno <moreno_at_mochima_dot_com@xxxxxxxxxxxxxx> writes:

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).

AFAICT, this impossible to do with a DSO (but easy with an archive).

Here is a (GNU-ld specific) solution:

- create libfoo.so as a linker script, containing

GROUP ( libfoo_nonshared.a libfoo_s.so )

- in libfoo_nonshared.a provide (in a single object) 'A::A()' and
'const int A::used = 1'

- in libfoo_s.so provide 'const int A::used = 0'

Now, any application that needs 'A::A()' and links against libfoo.so,
will "pull in" the object containing 'A::A()' from libfoo_nonshared.a,
and will get A::used == 1.

Any application that doesn't need A::A() will get A::used == 0
from libfoo_s.so

Now your puzzle is solved with:

if (A::used && !A::required_action_performed) {
// log error
}

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
.