Boost logo

Boost Users :

From: Ares Lagae (ares.lagae_at_[hidden])
Date: 2007-05-30 07:23:35


I want to call a functor for each type in a type list. Consider the
following program.

#include <iostream>

#include <boost/mpl/apply.hpp>
#include <boost/mpl/empty_base.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/joint_view.hpp>
#include <boost/mpl/lambda.hpp>
#include <boost/mpl/pair.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/vector.hpp>

template <typename T, typename Functor>
class functor_list_member
{
public:
  functor_list_member()
  {
    Functor f;
    f.template operator()<T>();
  }
};

template<typename Sequence, typename Functor>
class functor_list:
  private boost::mpl::fold<
    Sequence,
    boost::mpl::empty_base,
    boost::mpl::inherit<
      boost::mpl::_1,
      functor_list_member<boost::mpl::_2, Functor>
>
>::type
{};

typedef boost::mpl::vector<bool, char, short, int, long, float, double>
types;

struct functor
{
  template <typename T>
  void operator()()
  {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
  }
};

typedef functor_list<types, functor> my_functor_list;

int main(int argc, char* argv)
{
  my_functor_list();
}

The program outputs

void functor::operator()() [with T = bool]
void functor::operator()() [with T = char]
void functor::operator()() [with T = short int]
void functor::operator()() [with T = int]
void functor::operator()() [with T = long int]
void functor::operator()() [with T = float]
void functor::operator()() [with T = double]

However, I want to create a functor object first, and pass it to each
functor_list_member, like this:

template <typename T, typename Functor>
class functor_list_member
{
public:
  functor_list_member(Functor f)
  {
    f.template operator()<T>();
  }
};

template<typename Sequence, typename Functor>
class functor_list:
  private boost::mpl::fold<
    Sequence,
    boost::mpl::empty_base,
    boost::mpl::inherit<
      boost::mpl::_1,
      functor_list_member<boost::mpl::_2, Functor>
>
>::type
{
public:
  functor_list(Functor f)
    /* how do I call base(f) ? */
  {
  }
};

typedef boost::mpl::vector<bool, char, short, int, long, float, double>
types;

struct functor
{
  template <typename T>
  void operator()()
  {
    std::cout << __PRETTY_FUNCTION__ << std::endl;
  }
};

typedef functor_list<types, functor> my_functor_list;

int main(int argc, char* argv)
{
  functor f;
  my_functor_list(f);
}

but I do not know how to call the constructor of the base class in
functor_list::functor_list(Functor f). (see comment /* how do I call
base(f) ? */). Can someone help me?

Best regards,

-- 
Ares Lagae
Computer Graphics Research Group, Katholieke Universiteit Leuven
http://www.cs.kuleuven.be/~ares/

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net