Boost logo

Boost :

From: John Torjo (john_at_[hidden])
Date: 2003-05-23 05:00:57


Hi all,

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

It does not work for either VC6 nor for gcc3.2.
Do you have any idea of a workaround?

Here's the code:

#include <iostream>

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

public:
    struct internal {
        internal() {
            f();
        }
    };

    static internal s_internal;
};

template< class return_type, return_type (*f)()>
initializer< return_type, f>::internal initializer< return_type,
f>::s_internal;

// workaround for VC6 (VC6 does not handle void in generic programming very
well...)
template< void (*f)()>
class void_initializer {

public:
    struct internal {
        internal(int) {
            f();
        }
    };

    static internal s_internal;
};

template< void (*f)()>
void_initializer< f>::internal void_initializer< f>::s_internal = 0;

void f() {
    std::cout << "f()" << std::endl;
}

int main() {
    // does NOT call f() !!!
    void_initializer< &f> v;
    std::cin.get();
    return 0;
}

Thanks.

Best,
John

--
John Torjo
-- "Practical C++" column writer for builder.com.com
Freelancer, C++ consultant
mailto:john_at_[hidden]

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