
I'm trying to use the MPL for the first time, and I have two questions. First, I want to use BOOST_MPL_ASSERT to say that at least one of two conditions is true. For the time being, one of the conditions is always false, so this is what I've tried: BOOST_MPL_ASSERT(( false || boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints> )); It won't compile, so I tried this: BOOST_MPL_ASSERT(( mpl::or_(false, boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints>) )); It won't compile either. I'm guessing that I have to use mpl::false_ or something in place of false, but that doesn't work either. Can somebody please tell me the proper way to say what I want to say? My second question is about the existence of an MPL algorithm akin to the STL algorithm includes. Given two MPL vectors V1 and V2, I want to know if all the types in V1 are also in V2. I know about mpl::contains, but I can't find mpl::includes. Is there one, or do I need to write it myself? I have the MPL book, so references to that are fine, in addition to anything online. Thanks, Scott

BOOST_MPL_ASSERT operates on types, this also applies to mpl::or_ Try out: #include <boost/mpl/bool.hpp> #include <boost/mpl/assert.hpp> #include <boost/mpl/or.hpp> BOOST_MPL_ASSERT((true_)); //true is not a bool value it is a type it has underscore... BOOST_MPL_ASSERT(( mpl::or_(false_, boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints>) )); //false_ also a type... With Kind Regards, Ovanes -----Original Message----- From: Scott Meyers [mailto:usenet@aristeia.com] Sent: Freitag, 16. März 2007 20:38 To: boost-users@lists.boost.org Subject: [Boost-users] [MPL] 2 Newbie Questions I'm trying to use the MPL for the first time, and I have two questions. First, I want to use BOOST_MPL_ASSERT to say that at least one of two conditions is true. For the time being, one of the conditions is always false, so this is what I've tried: BOOST_MPL_ASSERT(( false || boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints> )); It won't compile, so I tried this: BOOST_MPL_ASSERT(( mpl::or_(false, boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints>) )); It won't compile either. I'm guessing that I have to use mpl::false_ or something in place of false, but that doesn't work either. Can somebody please tell me the proper way to say what I want to say? My second question is about the existence of an MPL algorithm akin to the STL algorithm includes. Given two MPL vectors V1 and V2, I want to know if all the types in V1 are also in V2. I know about mpl::contains, but I can't find mpl::includes. Is there one, or do I need to write it myself? I have the MPL book, so references to that are fine, in addition to anything online. Thanks, Scott _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Just an addition to the second question... Unfortunately, I forgot my MPL book at work :( But I remember, that in the chapter about physical quantities authors show how to compare quantity types (one which is iniaital mpl::vector like mpl::vector_c<int, 0, 1, 0, ...> and the other an instance of mpl::vector which is produced as a result of function application (which logically also results in the same template arguments but have different type)) this chapter might help you. With Kind Regards, Ovanes. -----Original Message----- From: Ovanes Markarian [mailto:om_boost@keywallet.com] Sent: Freitag, 16. März 2007 21:44 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [MPL] 2 Newbie Questions BOOST_MPL_ASSERT operates on types, this also applies to mpl::or_ Try out: #include <boost/mpl/bool.hpp> #include <boost/mpl/assert.hpp> #include <boost/mpl/or.hpp> BOOST_MPL_ASSERT((true_)); //true is not a bool value it is a type it has underscore... BOOST_MPL_ASSERT(( mpl::or_(false_, boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints>) )); //false_ also a type... With Kind Regards, Ovanes -----Original Message----- From: Scott Meyers [mailto:usenet@aristeia.com] Sent: Freitag, 16. März 2007 20:38 To: boost-users@lists.boost.org Subject: [Boost-users] [MPL] 2 Newbie Questions I'm trying to use the MPL for the first time, and I have two questions. First, I want to use BOOST_MPL_ASSERT to say that at least one of two conditions is true. For the time being, one of the conditions is always false, so this is what I've tried: BOOST_MPL_ASSERT(( false || boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints> )); It won't compile, so I tried this: BOOST_MPL_ASSERT(( mpl::or_(false, boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints>) )); It won't compile either. I'm guessing that I have to use mpl::false_ or something in place of false, but that doesn't work either. Can somebody please tell me the proper way to say what I want to say? My second question is about the existence of an MPL algorithm akin to the STL algorithm includes. Given two MPL vectors V1 and V2, I want to know if all the types in V1 are also in V2. I know about mpl::contains, but I can't find mpl::includes. Is there one, or do I need to write it myself? I have the MPL book, so references to that are fine, in addition to anything online. Thanks, Scott _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Ovanes Markarian wrote:
Try out:
#include <boost/mpl/bool.hpp> #include <boost/mpl/assert.hpp> #include <boost/mpl/or.hpp>
Missing #includes! Sigh. There's no <boost/mpl.hpp> to get everything, is there? Thanks for your help. They don't get much newbier than that, do they? Sounds like I'll have to write contains<TypeList1, TypeList2> myself, huh? Scott

Funny, but my girl friend just found the book in the shelf. On page 45-46 is an example with the quantities type checking. It relies on the mpl equal algorithm: http://www.boost.org/libs/mpl/doc/refmanual/equal.html It can test if two sequences have elements of the same type ;). But I have a feeling, that I misunderstand you. Would you like to check if TypeList2 have the elements of same type as TypeList1 or would you like to find out if TypeList2 is a subsequence of TypeList1, which can start somewhere in the middle of TypeList1 and go until the end? If you are looking for a susequence implementation it should not be so difficult to implement the contains meta function :). You can operate on iterator ranges and decide if one vector is a subsequence of another. mpl::iterator_range is also a model of forward, bidirection and random access sequence, so you can operate on that the subsequence vector and equal. Just in addition: I like the following doc link a lot: http://www.boost.org/libs/mpl/doc/refmanual/refmanual_toc.html Here you can find structured reference of supported mpl data structures and algorithms with examples. With Kind Regards, Ovanes -----Original Message----- From: Scott Meyers [mailto:usenet@aristeia.com] Sent: Freitag, 16. März 2007 23:48 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [MPL] 2 Newbie Questions Ovanes Markarian wrote:
Try out:
#include <boost/mpl/bool.hpp> #include <boost/mpl/assert.hpp> #include <boost/mpl/or.hpp>
Missing #includes! Sigh. There's no <boost/mpl.hpp> to get everything, is there? Thanks for your help. They don't get much newbier than that, do they? Sounds like I'll have to write contains<TypeList1, TypeList2> myself, huh? Scott _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Hi Scott, Scott Meyers wrote:
I'm trying to use the MPL for the first time, and I have two questions. First, I want to use BOOST_MPL_ASSERT to say that at least one of two conditions is true. For the time being, one of the conditions is always false, so this is what I've tried:
BOOST_MPL_ASSERT(( false || boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints> ));
It won't compile, so I tried this:
BOOST_MPL_ASSERT(( mpl::or_(false, boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints>) ));
It won't compile either. I'm guessing that I have to use mpl::false_ or something in place of false, but that doesn't work either. Can somebody please tell me the proper way to say what I want to say?
Since we're in the "type system world", there are no (unwrapped) constants - there are types and only types. So, there are no function calls, round parentheses and no operators - we have to use class templates, /metafunctions/, for the operations, because their specializations are types and they can hold a nested 'type' member for the result -- even templates and constants (but for returning lesser beings like that we use a 'type' member pointing back to the nesting class to have a proper metafunction). #include <boost/mpl/assert.hpp> #include <boost/mpl/bool.hpp> #include <boost/mpl/or.hpp> #include <boost/type_traits/is_same.hpp> using namespace boost; struct my_constant // to demystify mpl::false_ { static bool const value = false; typedef my_constant type; typedef bool value_type; }; typedef void type1; typedef void type2; BOOST_MPL_ASSERT(( mpl::or_< my_constant, mpl::false_, is_same<type1, type2> > )); Alternatively there's a non-MPL component, 'BOOST_STATIC_ASSERT', that takes a real constant. Using BOOST_STATIC_ASSERT we'd say: #include <boost/static_assert.hpp> #include <boost/type_traits/is_same.hpp> using namespace boost; typedef void type1; typedef void type2; BOOST_STATIC_ASSERT(( false || is_same<type1,type2>::value )); // Note: accessing nested value constant in is_same specialization // Also note: We'd have to say ::boost::is_same<type1,type2>::value // to please some broken compilers
My second question is about the existence of an MPL algorithm akin to the STL algorithm includes. Given two MPL vectors V1 and V2, I want to know if all the types in V1 are also in V2. I know about mpl::contains, but I can't find mpl::includes. Is there one, or do I need to write it myself?
I'd probably do it like this: #include <boost/mpl/vector.hpp> #include <boost/mpl/assert.hpp> #include <boost/mpl/count_if.hpp> #include <boost/mpl/not.hpp> #include <boost/mpl/contains.hpp> #include <boost/mpl/placeholders.hpp> using namespace boost; using namespace mpl::placeholders; typedef mpl::vector<int,char,float> seq1; typedef mpl::vector<double,float,char,int> seq2; // make sure all elements in seq1 are also in seq2 BOOST_MPL_ASSERT_NOT(( mpl::count_if< seq1, mpl::not_< mpl::contains<seq2, _> > > )); ...because I'm lazy. It's not optimal because we keep iterating 'seq1' even if we already found a mismatch. It can be fixed by using 'find_if' instead and comparing the result (its nested 'type' member) against the end iterator of 'seq1'.
I have the MPL book, so references to that are fine, in addition to anything online.
Still missing in my collection, unfortunately. Still waiting for the discount price together with "Effective MPL" ;-)... Regards, Tobias

on Fri Mar 16 2007, Scott Meyers <usenet-AT-aristeia.com> wrote:
I'm trying to use the MPL for the first time, and I have two questions. First, I want to use BOOST_MPL_ASSERT to say that at least one of two conditions is true. For the time being, one of the conditions is always false, so this is what I've tried:
BOOST_MPL_ASSERT(( false || boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints> ));
It won't compile
Generally C++ gets unhappy with you when you try to apply arithmetic operators to types. What you've done above is similar to BOOST_MPL_ASSERT(( false || int )) Try this (untested): BOOST_MPL_ASSERT(( boost::mpl::or_< boost::mpl::false_ , boost::is_same< CallerConstraints, CodeConstraints::Ignore_Constraints > > )) Two things to consider: * Are you missing a typename before CodeConstraints, or is Ignore_Constraints not a dependent type in this context? * Is is_same the appropriate test, or do you really want something like mpl::equal?
, so I tried this:
BOOST_MPL_ASSERT(( mpl::or_(false, boost::is_same<CallerConstraints, CodeConstraints::Ignore_Constraints>) ));
mpl::or_ is a metafunction just like all the others. You need the angle brackets.
It won't compile either. I'm guessing that I have to use mpl::false_ or something in place of false, but that doesn't work either. Can somebody please tell me the proper way to say what I want to say?
My second question is about the existence of an MPL algorithm akin to the STL algorithm includes. Given two MPL vectors V1 and V2, I want to know if all the types in V1 are also in V2. I know about mpl::contains, but I can't find mpl::includes. Is there one, or do I need to write it myself?
I have the MPL book, so references to that are fine, in addition to anything online.
// (untested) template <class OuterSequence, InnerSequence> struct includes : boost::is_same< typename mpl::find_if< InnerSequence , mpl::not_< mpl::contains<OuterSequence, _1> > >::type , typename mpl::end<SubSequence>::type > {}; In plain English, OuterSequence includes InnerSequence iff the result of trying to "find an element in InnerSequence that's not contained in OuterSequence" is the same as the InnerSequence's end iterator, i.e. no such element can be found. HTH, -- Dave Abrahams Boost Consulting www.boost-consulting.com

David Abrahams wrote:
template <class OuterSequence, InnerSequence> struct includes : boost::is_same< typename mpl::find_if< InnerSequence , mpl::not_< mpl::contains<OuterSequence, _1> > >::type , typename mpl::end<SubSequence>::type > {};
Okay, thanks very much. This works for me, and I even pretty much understand it :-). Unfortunately, I can't make the leap from this to what I really want, which is to provide a predicate to contains for determining whether two types are the same. I think I need to do something like this, and I apologize in advance for what will probably strike you as appalling: // returns whether Seq1 includes each of the elements in Seq2. // Pred defines whether 2 types are the same. template<typename Seq1, // the putative superset typename Seq2, // the putative subset typename Pred> // whether T1 from Seq1 == T2 from Seq2 struct includes_if : boost::is_same< typename find_if< Seq2, not_< is_same< find_if<Seq1, Pred::apply<_1 from outer find_if, _1 from inner find_if> > typename end<Seq1>::type > >::type, typename end<Seq2>::type
{}; I don't know how to refer to the current elements of the two calls to find_if, and I don't know whether it's a syntax issue or if I'm just approaching this incorrectly. Help? Thanks, Scott
participants (4)
-
David Abrahams
-
Ovanes Markarian
-
Scott Meyers
-
Tobias Schwinger