Boost logo

Boost :

From: Jonathan Turkanis (technews_at_[hidden])
Date: 2005-03-03 18:28:31


Hi,

<boost/shared_ptr.hpp> contains the following in the public section of the class
definition (reformatted):

    // Tasteless as this may seem, making all members public
    // allows member templates to work in the absence of member
    // template friends. (Matthew Langston)

    #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS

    private:

        template<class Y> friend class shared_ptr;
        template<class Y> friend class weak_ptr;

    #endif

        T * px; // contained pointer
        detail::shared_count pn; // reference counter

This has become a pretty common technique, I believe. I certainly use it a lot,
at any rate.

I'd like to suggest two macros which would allow code like the above to be
simplified as follows:

    BOOST_PRIVATE:
          BOOST_BEFRIEND_TEMPLATE(shared_ptr, Y, 1)
          BOOST_BEFRIEND_TEMPLATE(weak_ptr, Y, 1)

          T * px; // contained pointer
          detail::shared_count pn; // reference counter

Here BOOST_PRIVATE would expand to "private" for compilers that support template
friends and to "public" otherwise; BOOST_BEFRIEND_TEMPLATE would expand to a
template friend declaration where supported, and to nothing otherwise.

It might be desirable to have two macros BOOST_BEFRIEND_TEMPLATE_CLASS and
BOOST_BEFRIEND_TEMPLATE_STRUCT to avoid compiler warnings.

Jonathan


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