mlimber wrote:
> Right: the non-trivial initializer is the sticking point. Is this
> behavior regulated by the Standard? (Jonathan suggests not in his
> previous response.)
No idea.
> > Speaking of which, can't you put the reference to m_registered into the
> > template's constructor?
> >
> > template<class AbstractClass, class ConcreteClass>
> > class DefaultCreationPolicy
> > {
> > public:
> > // How about this!!
> > DefaultCreationPolicy()
> > {
> > (void) IsRegistered(); // touch that static member
> > }
> >
> > static AbstractClass* Create()
> > // ... etc ..
> >
> > Since the constructor is called somewhere, it drags in the dependency
> > on IsRegistered.
>
> Unfortunately, It doesn't seem to work on VC++ 6 or gnu 3.4.1 whether
> or not the constructor is inline.
Interesting. It seems that they realize that IsRegistered() does
``nothing''. After all, it only accesses a non-volatile variable and
throws away the result. Hey, since nothing else uses that variable, why
not throw it away---initializer with side effects be damned!
(I wonder if declaring that static member volatile would change the
behavior, hmm!)
In any case, you can do your own initialization, instead of relying on
the static variable's initializer.
Just replace the (void) IsRegistered() with EnsureRegistration().
EnsureRegistration() actually does something like this:
if (!s_registered)
s_registered = <the call to do the registration>;
A bit of construction-time overhead to check the flag, but hopefully,
the compiler won't throw this away.