Opened 15 years ago

Closed 15 years ago

#1732 closed Support Requests (fixed)

Require clarification on boost::serialization::singleton

Reported by: Sohail Somani <boost-trac@…> Owned by: Robert Ramey
Milestone: To Be Determined Component: serialization
Version: Boost Development Trunk Severity: Not Applicable
Keywords: Cc:

Description

Hi,

I would appreciate some reference to the standards in boost/serialization/singleton.hpp. Specifically why this is supposed to work on "working compilers":

BOOST_DLLEXPORT static T & instance; include this to provoke instantiation at pre-execution time static void use(T const &) {} static T & get_instance(){

static T t; refer to instance, causing it to be instantiated (and initialized at startup on working compilers) use(instance); return t;

}

Thanks!

Change History (2)

comment:1 by Sohail Somani <boost-trac@…>, 15 years ago

Argh. Foiled by wiki formatting. The formatted code snippet:

    BOOST_DLLEXPORT static T & instance;
    // include this to provoke instantiation at pre-execution time
    static void use(T const &) {}
    static T & get_instance(){
        static T t;
        // refer to instance, causing it to be instantiated (and
        // initialized at startup on working compilers)
        use(instance);
        return t;
    }

comment:2 by Robert Ramey, 15 years ago

Resolution: fixed
Status: newclosed

We have found no standards conforming way to guarentee instantiation of code not explicitly refered to. Implementation of BOOST_CLASS_EXPORT requires this facility. Hence, implementation of BOOST_CLASS_EXPORT not guarenteed portable.

However, we have been able to implement it on most widely used compilers. Typically this involves "exporting" for access by a DLL so that the compiler won't throw it away even though it's never explicitly referred to.

Of course, each compiler is different so its possible that moving this code from one compiler to another will require some sort of enhance to make it work. The current method factors this problem into one place - boost::serialization::singleton - whereas before it was sprinkled all over the place

Robert Ramey

Note: See TracTickets for help on using tickets.