Hi! I'd like to use MPL to remove elements of a set from another set. E.g.: using boost::mpl::set; struct a {}; struct b {}; struct c {}; typedef set<a, b, c> input; typedef set<b, c> remove_these; typedef boost::mpl::remove<input, remove_these>::type result; And result should be equal(?) to set<a>. Any idea how to do this? I'm a newbie on MPL terrain and it is quite brain-twisting... (I could use a beer to relief some strain) TIA Alfred -- Desperate Housewives - das Spiel! Pikante Skandale, schockierende Details unter: http://flat.games.gmx.de
AMDG Alfred Kohl wrote:
I'd like to use MPL to remove elements of a set from another set.
Try this: #include <boost/mpl/set.hpp> #include <boost/mpl/remove_if.hpp> #include <boost/mpl/has_key.hpp> #include <boost/mpl/equal.hpp> #include <boost/mpl/assert.hpp> #include <boost/mpl/insert.hpp> using boost::mpl::set; struct a {}; struct b {}; struct c {}; typedef set<a, b, c> input; typedef set<b, c> remove_these; struct set_inserter { typedef set<> state; typedef boost::mpl::insert<boost::mpl::_1, boost::mpl::_2> operation; }; typedef boost::mpl::remove_if<input, boost::mpl::has_key<remove_these, boost::mpl::_1>, set_inserter>::type result; BOOST_MPL_ASSERT((boost::mpl::equal<result, set<a> >)); int main() { } In Christ, Steven Watanabe
struct a {}; struct b {}; struct c {};
typedef set<a, b, c> input; typedef set<b, c> remove_these;
struct set_inserter { typedef set<> state; typedef boost::mpl::insert<boost::mpl::_1, boost::mpl::_2> operation; };
typedef boost::mpl::remove_if<input, boost::mpl::has_key<remove_these, boost::mpl::_1>, set_inserter>::type result;
BOOST_MPL_ASSERT((boost::mpl::equal<result, set<a> >));
Thanks alot! Works quite nicely. One thing I noticed: if input and remove_these do not share at least one common element compilation will fail (with vc8). E.g.: typedef set<a, b> input; typedef set<c> remove_these; BOOST_MPL_ASSERT((boost::mpl::equal<result, set<a, b> >)); It is not just that the assertion fails but the whole thing blows up (multiple pages of cryptic error messages). Is that expected? I can easily work around it by adding a dummy entry to both input and remove_these. Cheers, Alfred
AMDG Alfred Kohl wrote:
It is not just that the assertion fails but the whole thing blows up (multiple pages of cryptic error messages). Is that expected? I can easily work around it by adding a dummy entry to both input and remove_these.
What happens for me is just that the MPL assertion fires. The reason is that the order of elements in a set is unspecified, so equal doesn't work very well. Could you post the full code and the error message? In Christ, Steven Watanabe
participants (2)
-
Alfred Kohl
-
Steven Watanabe