Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2003-02-21 18:28:04


Peter Dimov wrote:
> > Problem is that I was trying to design a general boost::extract<>
> > facility, one that needs not be specialized for each type. (Indeed,
> > with my design, visitable types need not be cognizant of the facility
> > at all.)
>
> I missed that part. ;-)
>
> Still, I consider the function template interface better, even for a
general
> extract facility. Users that only extract from variants need not pay extra
> for a generic interface that they don't need.

I guess it depends if others see value in the generic interface. Let me
explain how things now stand...

Currently, extract<> works for any type that is visitable by means of the
following visitor (simplified for demonstration):

  template <typename T> class extracting_visitor
      : public static_visitor<T*>
  {
      T* operator()(T& operand)
      { return boost::addressof(operand); }

      template <typename U> T* operator()(U&)
      { return static_cast<T*>(0); }
  };

Of course, a non-visitable type may too support extraction, but it must then
explicitly inherit boost::extractable or specialize
boost::extractable_traits.

Though the only visitable type right now is variant, in the sandbox I have
implemented visitation for boost::any. Thus, without further changes to
boost::any, it would too support extract<>.

I guess the real question we need to answer is whether these semantics of
extraction-by-visitation is really appropriate. If not, then I guess
function templates particular to variant could be introduced. (Note,
however, that the similar facility for boost::any will look nearly
identical, which to me suggests some value in a generic interface.)

What do others think?

> You can make a generic function template interface by using
metaprogramming,
> but I don't know how well the compilers will handle it.

What do you have in mind? Before changing to the present form, extract<>
used to look like this:

  template <typename T, typename Extractable>
    typename mpl::apply_if<
          is_pointer<Extractable>
        , add_pointer<T>
        , add_reference<T>
>::type
  extract(Extractable& operand)
  {
    ...
  }

With a function taking a reference-to-const, it works perfectly on g++.
However, MSVC chokes on this form.

Thanks,
Eric


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