Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-12-15 12:13:20


I think that a compile-time port of boost.bind would be a valuable addition
to MPL.

See the end of the message for a starting point. This works on MSVC; a full
port with nested binds and a bind<> syntax (not the numbered bind0, bind1,
... variants) would be possible, too, and is actually not difficult on a
conforming compiler. On MSVC it'd be a bit harder, but doable.

Comments?

--
Peter Dimov
Multi Media Ltd.
#include <iostream>
#include <typeinfo>
// bind
struct _1;
struct _2;
struct _3;
struct _4;
struct _missing;
namespace _bi
{
template<class T> struct select
{
 template<class B1, class B2, class B3, class B4> struct apply
 {
  typedef T type;
 };
};
template<> struct select<_1>
{
 template<class B1, class B2, class B3, class B4> struct apply
 {
  typedef B1 type;
 };
};
template<> struct select<_2>
{
 template<class B1, class B2, class B3, class B4> struct apply
 {
  typedef B2 type;
 };
};
template<> struct select<_3>
{
 template<class B1, class B2, class B3, class B4> struct apply
 {
  typedef B3 type;
 };
};
template<> struct select<_4>
{
 template<class B1, class B2, class B3, class B4> struct apply
 {
  typedef B4 type;
 };
};
}
template<class F, class A1, class A2> struct bind2
{
 typedef bind2 type;
 template<class B1 = _missing, class B2 = _missing, class B3 = _missing,
class B4 = _missing> struct apply
 {
  typedef typename _bi::select<A1>::template apply<B1, B2, B3, B4>::type a1;
  typedef typename _bi::select<A2>::template apply<B1, B2, B3, B4>::type a2;
  typedef typename F::template apply<a1, a2>::type type;
 };
};
// test
template<int I> struct value_wrapper
{
 enum { value = I };
};
struct less
{
 template<class A, class B> struct apply
 {
  enum { value = (A::value < B::value) };
  typedef value_wrapper<value> type;
 };
};
int main()
{
 typedef bind2<less, _1, value_wrapper<5> >::type bound;
 typedef bound::apply< value_wrapper<2> >::type result;
 std::cout << typeid(result).name() << '\n';
}

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