23 Sep
2005
23 Sep
'05
8:08 a.m.
David Hall wrote: > Tzu-Chien Chiu wrote: > >> Any boost library can make me write the expression like >> >> int x = 3; >> if ( x in list(3, 5, 6, 7) ) { >> ... >> } >> >> >> > I don't know about the boost libraries, but I quickly sketched out > something you might be interested in, based on something I saw on one of > the news groups. All you have to do is to write a binary predicate > functor with bool operator()(int,). It's > untested, but a quick syntax check with g++ 3.4.2 works fine. > > Usage will be > if (x list(3,5,6,7) ) { ... } > > I hope that's more or less what you're after; I don't know if you want > that list type/function as well. > > The code itself is attached. Be sure to fix the template parameter in > the unnamed namespace.. > > David Hall > > > ------------------------------------------------------------------------ > > _______________________________________________ > Boost-users mailing list > Boost-users@lists.boost.org > http://lists.boost.org/mailman/listinfo.cgi/boost-users Huh.. I never thought to overload them that way, I like it! Some small issues with the implementation: * in operator_t : your ctor init is... dodgy at best. Rename the parameter _f or something (ie: operator_t(F _f = F()) : f(_f) {}) * Cleaner layout! indent evenly, return types and init lists on their own lines, not seperating & from types, spaces after comma's in parameter lists, etc... Makes comprehension easier, for you _and_ your readers. * related note: more meaningful template parameter names: BinFn instead of F, ValueT instead of T, etc... * It should be designed as an expression template library, 'in' is something that operator< can recognise as an infix op, and generate some binder functional that when it sees an operator> can call the op on the values to its left (stored) and right. I'd have to take a look at what some of the expression template functions do to come up with an implementation. This way works for everything, and, if you are careful, you could generalise to pretty much operator that you want, and the same operator< template function can create the right thing. I'll play around with it over the weekend and see what I can come up with. Boost.Infix?