Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2003-05-23 11:43:39


On Fri, 23 May 2003 13:00:57 +0300, "John Torjo" <john_at_[hidden]>
wrote:

>Hi all,
>
>While working on smart_assert, I created a class that should ensure calling
>a global function before main (a static_initializer).

Well, about "ensuring"... 3.6.2/3 of the standard.

>
>It does not work for either VC6 nor for gcc3.2.

What compilers does it work with?

>Do you have any idea of a workaround?
>

Relying on dynamic initialization of an object at namespace scope puts
you under the limitations of 3.6.2/3. Under those conditions you can
simply do

  const int n = (f(), 1);

without any helper class or class template. If there are reasons to
use the class template, as you did, it's important to consider that
the standard prohibits instantiations of template static data members
(and nested classes) definitions that are not "necessary" (see
14.7.1). One way to force that instantiation is to take the address of
the static data member. Another way should be to explicitly specialize
the static data member itself for the function f (I'm not 100% sure
though):

  template< class return_type, return_type (*f)()>
  class initializer {

  public:
    // note the dummy parameter: 14.7.3/15
    initializer(int dummy) { f(); }
  private:
    static initializer s_internal;
  };

  template<>
  initializer< void, f> initializer< void, f>::s_internal = 1;

Genny.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk