Boost logo

Boost Users :

Subject: Re: [Boost-users] [type_erasure] BOOST_TYPE_ERASURE_MEMBER with template?
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2015-08-15 12:38:00


On 08/15/2015 10:00 AM, Nat Goodspeed wrote:
> The code with which I'm working has two similar template classes: they
> have a common subset of 8 methods that could be used on an instance of
> either. For performance reasons, these methods are not virtual. As far
> as the compiler knows, the two template classes are unrelated.
> Naturally we can write a template function to accept an instance of
> either template class, as long as it uses only methods from the common
> subset.
> I also want to support runtime polymorphism, permitting a non-template
> function to accept an instance of either template class. Obviously
> that will incur some performance hit, but the coder can decide whether
> or not his/her function is performance-critical. If so, s/he can write
> that function to accept one or the other specific template class -- or
> write a template function.
> We want to incur the performance hit only when the coder deems that an
> acceptable tradeoff. The classic solution would be to derive both
> template classes from a common base class and make the common methods
> virtual -- but then that penalizes every consumer.
> I haven't yet used Boost.TypeErasure, but it seems to me potentially
> applicable to this problem.
> I see the example[0] illustrating use of BOOST_TYPE_ERASURE_MEMBER().
> I was going to define requirements for each of the 8 common methods
> ... but then I became confused. Our common methods use a template
> parameter from the parent template class as a method parameter type.
> Can I invoke BOOST_TYPE_ERASURE_MEMBER in a generic way that would
> support all valid template parameters?
> Two of the 8 common methods are template methods, accepting a
> parameter whose type is a template parameter for the method rather
> than the containing class. I don't know whether that introduces
> further complexity.

So, your interface looks something like this:

template<class T>
class A {
    void f();
    void g(T);
    template<class U>
    void h(U);

To handle g, you need to treat T as an
associated type.

In order to handle the template h, you need
to type erase U like this:

In Christ,
Steven Watanabe

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at