Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-02-18 12:19:08


Eric Friedman wrote:
>
> In the past we sought to support the following...
>
> variant<...> var;
> T* p = extract<T>(&var);
> T& r = extract<T>(var);
>
> ...but it had to be dropped because (essentially) ambiguity exists
> between the following:
>
> template <typename T, typename Extractable>
> T& extract(Extractable & operand);
> template <typename T, typename Extractable>
> T* extract(Extractable * const operand);

This is only ambiguous in its most general form given above. If you rewrite
it as

template <class T, ...> T & extract(variant<...> & operand);
template <class T, ...> T const & extract(variant<...> const & operand);
template <class T, ...> T * extract(variant<...> * operand);
template <class T, ...> T const * extract(variant<...> const * operand);

it's no longer ambiguous.

The other option is to support a dynamic_cast-style extract:

T * p = extract<T*>(&var);
T & r = extract<T&>(var);

but it would likely require partial ordering/specialization.

"extract" is probably not the best name in both cases since it doesn't
extract, it returns a reference to the internal representation. The
documentation needs to describe when this reference is invalidated.


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