Boost logo

Boost Users :

Subject: [Boost-users] [type_erasure] BOOST_TYPE_ERASURE_MEMBER with template?
From: Nat Goodspeed (nat_at_[hidden])
Date: 2015-08-15 12:00:29

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

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.

Thank you for any help you can provide!


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