$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
Subject: Re: [Boost-users] [multi-index] Container of polymorphic pointers
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2008-10-30 03:10:39
Daniel Dilts <diltsman <at> gmail.com> writes:
> I'm am using boost::multi_index to hold a set of polymorphic entities.
> Due to efficiency concerns, I have to be able sort the container based
> on actual type.  I was planning on sorting based on the return result of
> typeid.  When I try this I get errors in <functional>.  I am using
> VC++ 2005 Standard Edition SP1.  What am I doing wrong, or, what is a
> better way of accomplishing what I am trying to do?
> 
[...]
>     struct extractType
>     {
>         typedef const type_info& result_type; 
Here is the first problem: you've got to define
    typedef type_info result_type
and let operator() return value be const result_type& (instead
of the current return_type).
Past the first hurdle you'll find that type_info is not comparable
with <, so you'll also need to provide a custom comparer for this type
based on type_info::before. The following summarizes all the changes
needed (note: do not forget to include <typeinfo>; also, you're using
type_info instead of std::type_info, which seems to work in VS but is not
standard as far as I know):
namespace Entities
{
    struct type{};
    struct extractType
    {
        typedef std::type_info result_type; 
        const result_type& operator()(const Entity * r)const
        {
            return typeid(*r);
        }
    };
    struct lessType
    {
      bool operator()(const std::type_info& x, const std::type_info& y)const
      {
            return x.before(y);
      }
    };
    typedef boost::multi_index::multi_index_container<
        Entity *,
        boost::multi_index::indexed_by<
            boost::multi_index::ordered_non_unique<
                boost::multi_index::tag<type>, extractType, lessType
            >
        >
    > EntityContainer;
};
Hope this helps. Thank you for using Boost.MultiIndex.
JoaquÃn M López Muñoz
Telefónica, Investigación y Desarrollo