From: Philippe A. Bouchard (philippeb_at_[hidden])
Date: 2003-02-27 09:28:14


"Philippe A. Bouchard" <philippeb_at_[hidden]> wrote in message
news:b3hcea$gri$1_at_main.gmane.org...

[...]

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

BTW implicit cast to reference types are not implicit under GCC, they have
to be called explicitly. Template casts do not seem to be defined in the
standard...:

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_)));
            }

    ...
 };

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

int main()
{
    optional<B> b;

    //foo(b);
    foo(b.template operator optional<A> const & <A> ());
}

Also, the advantage of casts to reference types is that they do not create
any temporary objects implicitly. I do not see in what it can be dangerous.

What do you think?

Philippe A. Bouchard