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



Carlos Moreno <moreno_at_mochima_dot_com@xxxxxxxxxxxxxx> writes:

Paul Pluzhnikov wrote:

Here is a (GNU-ld specific) solution:

Why do I have such guarantee??

Nothing in life is guaranteed, except death and taxes :-)

Don't these definitions clash if
for some reason I need *both* linked?

You will *always* have both linked -- you want to have absolute
minimum of code in the non-shared libfoo_nonshared.a, so you'll
put one data item there (A::used), and also one function the
constructor.

If anyone references the constructor (even in the

if (false) { .... }

section [1]), then you'll get both the constructor, and the data
(because linker doesn't "sub-divide" objects when it pulls them
from archive -- you get all or nothing.

The reason you don't get multiply-defined symbol error [2] is
that UNIX shared libraries are explicitly designed to emulate
archive libraries, and with archives, if you put need definition
info libbar.a and put libbar.a before libfoo.a on the link line,
then libbar.a definition will be used.

Actually, you could eliminate the A::used definition in libfoo_s.so
altogether, by using "weakly-unresolved" reference:

extern int A::used __attribute__((weak));

Then you'll have to modify the condition:

if (&A::used && A::required_action_not_performed) {
}

It may appear that '&A::used' is always true (and some versions of
gcc warn about it), but it's not.

Cheers,

[1] Note that some versions of gcc will optimize out even the
call to the constructor, if gcc can prove that the condition is
always false.

But then, does this program really reference A ?

[2] I did not actually test this, but I am fairly certain it works.
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
.



Relevant Pages

  • Re: a method to make js have the ability to inherit
    ... but without the implied type-conversion of the string ... that uses the name of a specific constructor. ... programmer has no idea at all what types of object they are ... no reason for ever doing so. ...
    (comp.lang.javascript)
  • Another base/derived problem with gcc, but not with MSVC
    ... When i compile the program listed below with gcc version 3.3.1 (MinGW on ... 'base' copy constructor ... Is it the gcc or the MSVC compiler that is broken? ... int main (int argc, char* argv) ...
    (comp.lang.cpp)
  • Re: Structs cannot contain explicit parameterless constructors
    ... I don't know what the real reason is but it may have something ... constructor it would not be called unless you recompiled. ... much influence on the language design. ... IdentityStamp id = obj.Id; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Copy constructor needed I think, maybe some op= as well?
    ... > In your case that means: Why on earth do you allocate the Viewport object ... > in RadioButton dynamically? ... As far as I can see there is no good reason ... > happens to the pointer inner_button, if the default constructor is used?) ...
    (comp.lang.cpp)
  • Re: Question about &rest, &key, and &allow-other-keys
    ... The reason I don't just put the default arguments into the constructor ... for the call is that I need a bunch of different "sets of defaults". ... the additional ability to specify individual parameters manually. ...
    (comp.lang.lisp)