Boost logo

Boost :

From: Philippe A. Bouchard (philippeb_at_[hidden])
Date: 2003-02-25 22:31:36


David B. Held wrote:

[...]

> and then just store Base*, but this requires explicit run-time
> checking of types which would otherwise be automatic with pointers to
> T instead of optional<T>. If this is the only solution, so be it;
> but any clever alternatives are welcome.

This example is not objective at all, but I think it would be possible to
add a convertion operator which will decide if the cast is acceptable or
not. It is not meant for pointers, but reference types:

struct A
{
    virtual ~A() {}
};

struct B : A
{
};

struct C
{
};

template <typename T>
    struct optional
    {
        template <typename U>
            operator optional<U> const () const
            {
                return * reinterpret_cast<optional<U> const *>(static_cast<U
const *>(reinterpret_cast<T const *>(storage_)));
            }

    ...
    };

void foo(optional<A> const &)
{
}

int main()
{
    optional<B> b;
    optional<C> c;

    foo(b);
    //foo(c);
}

Philippe A. Bouchard


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