Topic: definition for static member


Author: gp1@paradise.net.nz (Graeme Prentice)
Date: Sat, 23 Aug 2003 19:21:55 +0000 (UTC)
Raw View

Is the following code legal according to the 1998 standard or defect
reports or pending active issues?  The code compiles with no errors in
G++, Comeau and Borland even though the static member m1 of struct s2
has no out of class definition.

I would like a compile time error for this because I have some
initialisation that I want to occur when the m1 member of s2 is
constructed, however I realise it's probably convenient in the
majority of cases that no out of class definition is required  -  so
instead, I would like the standard to define whether it's legal or not
if it doesn't already.

If I provide an out of class definition for the m1 member of s2
(uncomment line *1), is the compiler required to call the constructor
for the m1 object   - if so, where does the standard say so?

Is there any way to guarantee some initialisation occurs (that
operates on the static members of s1 via the callback to s2::f1 ) for
the m1 member of s2 without imposing any overhead on the s1::f1()
function I am calling from main and without changing main() or the s2
class?

In my actual (real) code, s1 is a template class and s1::k2 is a
vector and I want to add items to the vector via the callback.

Graeme


#include <iostream>

struct s1
{
    static int f1() { return k2; }
    static int k2;
    s1( int (*fp)() ){ k2 = fp();  }
};

int s1::k2;

struct s2
{
    static s1 m1;
    static int f1() { return 99; }
};

// s1 s2::m1( s2::f1 );  // *1

int main()
{
    s2 a1;
    std::cout << a1.m1.f1();
}

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]