[MPL] Filtering a sequence to leave only most-derived types

Hi, I've got an MPL sequence containing a (rather small) number of classes. I want to filter this sequence so that all classes that are a base of another class in the sequence are filtered out. I thought I'd do something like this (in pseudo-code): is_strict_base_of(Base, Derived) = !is_same(Base, Derived) && is_base_of(Base, Derived) Result = remove_if( Interfaces, find_if( Interfaces, is_strict_base_of(outer_value, inner_value) ) != end(Interfaces) ) where outer_value is the iteration value of the remove_if loop, and inner_value is the iteration value of the count_if loop. Yes, this is N², but since the length of the initial sequence is very unlikely to exceed 8, I can live with that. In MPL syntax: template <typename Base, typename Derived> struct is_strict_base_of : mpl::and_< mpl::not_<boost::is_same<Base, Derived> >, boost::is_base_of<Base, Derived> > {}; typedef typename mpl::remove_if< Interfaces, // Remove from Interfaces mpl::not_<mpl::equal< mpl::find_if< Interfaces, // if there is any value in interfaces // that this is a base of is_strict_base_of<???> >, mpl::end<Interfaces> > >
::type Bases;
How do I represent outer_value and inner_value in this expression? It seems I can't simply put mpl::_1 and mpl::_2 there, since that gives me a static assertion failure (argument is not available). I think I have to put an apply somewhere in there, but I can't figure out where. Sebastian

Sebastian Redl wrote:
How do I represent outer_value and inner_value in this expression? It seems I can't simply put mpl::_1 and mpl::_2 there, since that gives me a static assertion failure (argument is not available). I think I have to put an apply somewhere in there, but I can't figure out where.
Sebastian
Not sure, but I would do something like typedef mpl::vector< A, B, C > m_vec; typedef mpl::sort< m_vec, boost::is_base_of< mpl::_1, mpl::_2 >
::type sorted_type;
typedef mpl::unique< sorted_type, boost::is_base_of< mpl::_1, mpl::_2 >
::type unique_type;
(untested, but I have the idea that it is close to what you would like it to do :-)) Cheers, Rutger

Rutger ter Borg <rutger <at> terborg.net> writes:
Not sure, but I would do something like
typedef mpl::vector< A, B, C > m_vec;
typedef mpl::sort< m_vec, boost::is_base_of< mpl::_1, mpl::_2 >
::type sorted_type;
typedef mpl::unique< sorted_type, boost::is_base_of< mpl::_1, mpl::_2 >
::type unique_type;
Thank you, I got to the solution with this. I considered using sort+unique at first, but dismissed it because the predicate does not define a total order, and because is_base_of is not a proper equivalence.. But apparently it works anyway. I just had to reverse the condition (i.e. define a is_derived_from metafunction). Sebastian
participants (2)
-
Rutger ter Borg
-
Sebastian Redl