Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2005-02-08 05:32:08


Nagender Parimi <nkparimi_at_[hidden]> writes:

> Hi,
>
> I am looking for a static list of types, albeit one in which the order
> of types does not matter. I have looked into the Metaprogramming
> library (MPL) and some related work in BGL on property lists, but
> could not find anything that would make the list unordered (though
> they do provide a static type-list). To clarify what I mean, using the
> MPL:
>
> typedef boost::mpl::vector<float, int> type1;
> typedef boost:mpl::vector<int, float> type2;
>
> the above usage makes type1 and type2 distinct types since the
> ordering of types matters. Is there a library compenent with which I
> can construct a static list of types (just like usage of
> boost:mpl::vector above) and can query for presence or absence of a
> type, making the order irrelevant?
>
> Furthermore, I would prefer this presence/absence check to be resolved
> compile-time. The reason for this requirement is that I wish to use
> this presence/absence test as means by which the compiler can call the
> appropriate specialized functions for this static list.

Sounds like a job for mpl::set. You can Instead of using mpl::equal, to check
that two sets have the same elements you can use:

  // true iff s1 is a superset of s2
  template <class s1, class s2>
  struct is_superset
    : boost::is_same<
          mpl::find_if<
              s1
            , mpl::not_< mpl::has_key<s2,_> >
>::type
        , mpl::end<s1>::type
>
  {}

  mpl::and_<
      mpl::equal_to<
          mpl::size<s1>
        , mpl::size<s2>
>
    , has_all<s1,s2>
>::type

[Say, Aleksey, shouldn't contains<s,k> be a synonym for has_key<s,k>
when s is a set? This example really makes me dream about Vesa's
fully-lazy system. It's a bit annoying to have to write out
is_superset as a separate metafunction.]

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk