Boost logo

Boost :

From: Aleksey Gurtovoy (alexy_at_[hidden])
Date: 2001-06-23 12:33:23


 Douglas Gregor wrote:
> It appears that, even though STLport has a conforming
> allocator, MSVC won't allow use of it. The 'rebind'
> member template, which is conspicuously absent
> from the standard library that ships with MSVC, cannot be
> used so far as I can tell. The standard usage (template
> parameters Allocator and T):
>
> typedef typename Allocator::template rebind<T>::other
> t_allocator_type;
>
> causes an internal compiler error. I was not able to find a
> real workaround.

This should work, but please don't tell anybody that I am the author of this
"code" :).

namespace detail {
template<class T>
struct msvc_never_true
    {
    struct fake_type;
    BOOST_STATIC_CONSTANT(bool, value =
        (boost::is_same<T, fake_type>::value)
        );
    };
}

template<typename Allocator, typename T>
struct foo
    {
    template<bool> struct Allocator_wrapper : Allocator {};
    template<> struct Allocator_wrapper<true> : Allocator
        {
        template<class> struct rebind;
        };
    
    typedef typename Allocator_wrapper<
        detail::msvc_never_true<Allocator>::value
>::template rebind<T>::other allocator_type;
    };

It's not even a legal C++, but MSVC seems to be much more happy with it than
with the original construct... anyway, it works, and I can even try to
explain why, but only if you _really_ want to know :). Failures on
constructs like this were real showstoppers to implementing 'boost::mpl' on
MSVC, until I found the above workaround. In fact, 'boost::mpl' library uses
the workaround often enough to define a macro which encapsulates it
(BOOST_MPL_DEPENDENT_TEMPLATE_TYPEDEF, 'boost/mpl/mpl_config.hpp' header
from the http://groups.yahoo.com/group/boost/files/mpl/mpl-apr-28-01.zip
archive). May be it's worth to be moved to 'boost/config.hpp' (with an
appropriate name change). Code that uses the macro still won't be very
readable:

template<typename Allocator, typename T>
struct foo
    {
    BOOST_MPL_DEPENDENT_TEMPLATE_TYPEDEF(Allocator, rebind, T,
rebind_result);
    typedef typename rebind_result::other allocator_type;
    };

but at least this way we can keep a particular hack that was used to make
this thing work in one place (BTW, I wonder if it still works under MSVC 7.0
beta 2, anyone?).

Aleksey


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