Boost logo

Boost :

Subject: Re: [boost] Formal Review Request: TypeErasure
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2012-05-30 06:18:49


AMDG

On 05/30/2012 01:30 AM, Thomas Petit wrote:
> <snip>
> Now what are the minimal, non-intrusive modifications using Boost.TypeErase
> we have to make so we can write in a value-based style, with regular types ?
> Can we achieve something like this ? :
>
> --------------
> std::vector<any<printable>> printers;
> printers.push_back(separator_printer(","));
> printers.push_back(column_separator_printer(",", 4));
> for(any<printable>& printer : printers)
> {
> printer->print(std::cout, test);
> }
> -------------
>

template<class T = _self>
struct printable {
  static void apply(const T& t,
                    std::ostream& os,
                    const std::vector<int>& v)
  { t.print(os, v); }
};

namespace boost { namespace type_erasure {
template<class T, class Base>
struct concept_interface< ::printable<T>, Base, T> : Base
{
    void print(std::ostream& os, const std::vector<int>& v) const
    {
        call(printable<T>(), *this, os, v);
    }
};
}}

The two differences in usage are that you have
to use printable<> and printer.print(std::cout, test).

In Christ,
Steven Watanabe


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