Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2003-05-25 05:59:54

On Sun, 25 May 2003 10:40:37 +0300, "John Torjo" <john_at_[hidden]>

>> [...]
>> What compilers does it work with?
>None I have tested it with ;-)

Didn't you suspect this question was rhetorical?

>> >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);
>I suppose you mean at global scope.

No. I meant namespace scope, which is a more general concept than
global scope.

>But if this is within a header file, f() might be called multiple times.
>What I want, is f() to be called ONCE, before main().

You didn't say that. In your original example you constructed a

  void_initializer< &f>

right in main, so it wasn't all contained in headers either.

>> 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):
>3.6.2/3 - footnote 31)
>An object defined in namespace scope having initialization with sideeffects
>must be initialized even if it is not used (3.7.1).

The point is that no object is defined in your case. The definition is
not even generated. The case is different if you have a non-template

  struct X {

      static int n;

  int X::n = (g(), 1);

>So, I used a volatile:
>---------- code
>---------- ENDOF code
>I've probably misunderstood something,

Indeed. I thought it was clear from the paragraph I pointed you to
(14.7.1) that with your original code no *definition* of the static
data member is generated (roughly speaking, implicit instantiation of
a template requires instantiating the *declarations* of all the
members - however definitions are not instantiated if not needed).
BTW, I don't even see why you used a nested class. Are you aware that
this is legal?

  class A {

   static A a;

> since f() is still not called.
>I've tested it with VC6/gcc3.2.
>Can anybody test it with other compiler(s), and let me know if it works?

I haven't looked at your second attempt. However, if it is still based
on the static data member definition then it doesn't work on
conforming compilers.

P.S.: I think, unless we bind it to a specific library design problem,
this is off-topic here. So either we make it clear why this problem
arose in relation to smart_assert, or move the discussion to c.l.c++.m


Boost list run by bdawes at, gregod at, cpdaniel at, john at