From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2008-03-28 18:47:09
Emil Dotchevski wrote:
> The typical use of a variant is to hold objects of types with value
> semantics. Most such types are not polymorphic and so dynamic RTTI can
> not be used to tell them apart.
Boost.variant was cited as an example of a system providing visitation.
As in the describing code I wrote earlier, we are here talking of a free
function, unrelated to variant.
(Of course, variant could eventually try to use this to optimize some
cases, memory usage-wise)
The motivation is that very often, I've seen code that wanted to do
different things based on the actual type of a polymorphic object, in
certain cases at least. They either use quite a few dynamic_casts, which
is inefficient, or use their own RTTI mechanism to do an ugly switch
with downcasts. Or, if they really want to do different things in all
cases, they use the heavyweight Visitor design pattern.
I thought it was a shame to have to write your own RTTI system to do
that. Due to the limitations of the standard RTTI system, though, it is
required to do something a bit ugly and not as efficient as it could be,
Maybe static visitation isn't much more concise and powerful than the
visitation pattern, and thus not worth the hassle.
> Could you provide more info about your specific use case? Could you
> use boost::any and dispatch based on RTTI?
Why not, the function could be extended to work with any polymorphic
object or object which has a type_info& type() member function or free
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk