[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
::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
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